如何用命令行curl显示请求头信息

命令行curl可以通过使用-D选项显示响应头,但我想看看它发送的是什么请求头。我怎样才能做到这一点呢?

curl的-v--verbose选项显示了HTTP请求头,以及其他东西。下面是一些样本输出。

$ curl -v http://google.com/
* About to connect() to google.com port 80 (#0)
*   Trying 66.102.7.104... connected
* Connected to google.com (66.102.7.104) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.16.4 (i386-apple-darwin9.0) libcurl/7.16.4 OpenSSL/0.9.7l zlib/1.2.3
> Host: google.com
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Location: http://www.google.com/
< Content-Type: text/html; charset=UTF-8
< Date: Thu, 15 Jul 2010 06:06:52 GMT
< Expires: Sat, 14 Aug 2010 06:06:52 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 219
< X-XSS-Protection: 1; mode=block
< 
<meta http-equiv="content-type" content="text/html;charset=utf-8">
301 Moved
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.

* Connection #0 to host google.com left intact
* Closing connection #0
评论(2)

我相信你要找的传递给curl的命令行开关是-I

使用实例。

$ curl -I http://heatmiser.counterhack.com/zone-5-15614E3A-CEA7-4A28-A85A-D688CC418287  
HTTP/1.1 301 Moved Permanently
Date: Sat, 29 Dec 2012 15:22:05 GMT
Server: Apache
Location: http://heatmiser.counterhack.com/zone-5-15614E3A-CEA7-4A28-A85A-D688CC418287/
Content-Type: text/html; charset=iso-8859-1

此外,如果你遇到一个响应的HTTP状态代码为301,你可能也想传递一个-L参数开关,告诉curl跟随URL重定向,在这种情况下,打印所有页面的标题(包括URL重定向),如下图所示。

$ curl -I -L http://heatmiser.counterhack.com/zone-5-15614E3A-CEA7-4A28-A85A-D688CC418287
HTTP/1.1 301 Moved Permanently
Date: Sat, 29 Dec 2012 15:22:13 GMT
Server: Apache
Location: http://heatmiser.counterhack.com/zone-5-15614E3A-CEA7-4A28-A85A-D688CC418287/
Content-Type: text/html; charset=iso-8859-1

HTTP/1.1 302 Found
Date: Sat, 29 Dec 2012 15:22:13 GMT
Server: Apache
Set-Cookie: UID=b8c37e33defde51cf91e1e03e51657da
Location: noaccess.php
Content-Type: text/html

HTTP/1.1 200 OK
Date: Sat, 29 Dec 2012 15:22:13 GMT
Server: Apache
Content-Type: text/html
评论(4)

verbose选项很方便,但如果你想看到curl所做的*一切(包括传输的HTTP正文,而不仅仅是头文件),我建议使用以下选项之一。

  • --trace-ascii - # stdout
  • --trace-ascii output_file.txt # 文件
评论(1)