如何使用命令行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
< 
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
* Connection #0 to host google.com left intact
* Closing connection #0

curl -sD - -o /dev/null http://example.com
  • -s - 避免显示进度条
  • -D - - 将标题转储到文件,但是-将其发送到标准输出
  • -o /dev/null - 忽略响应主体
  • 这比-I更好,因为它不发送HEAD请求,这可能产生不同的结果。

    它比-v更好,因为你不需要太多的黑客来解除它。


    我相信你正在寻找传递给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
    
    链接地址: http://www.djcxy.com/p/41345.html

    上一篇: How to display request headers with command line curl

    下一篇: Get HTTP Body of Form in JavaScript