可以使用HTTP HEAD只请求报头,如curl(1)中的选项-I。

$ curl -I /

在命令行中获取冗长的HTML响应体是一件痛苦的事情,因此我希望只获得POST请求的报头作为反馈。但是,HEAD和POST是两种不同的方法。

如何让cURL只显示POST请求的响应头?


当前回答

-D, --dump-header <file>
       Write the protocol headers to the specified file.

       This  option  is handy to use when you want to store the headers
       that a HTTP site sends to you. Cookies from  the  headers  could
       then  be  read  in  a  second  curl  invocation by using the -b,
       --cookie option! The -c, --cookie-jar option is however a better
       way to store cookies.

and

-S, --show-error
       When used with -s, --silent, it makes curl show an error message if it fails.

and

-L/--location
      (HTTP/HTTPS) If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response
      code), this option will make curl redo the request on the new place. If used together with -i/--include or -I/--head, headers from  all  requested
      pages  will  be  shown.  When authentication is used, curl only sends its credentials to the initial host. If a redirect takes curl to a different
      host, it won’t be able to intercept the user+password. See also --location-trusted on how to change this. You can limit the amount of redirects to
      follow by using the --max-redirs option.

      When curl follows a redirect and the request is not a plain GET (for example POST or PUT), it will do the following request with a GET if the HTTP
      response was 301, 302, or 303. If the response code was any other 3xx code, curl will re-send the following  request  using  the  same  unmodified
      method.

从手册页。所以

curl -sSL -D - www.acooke.org -o /dev/null

遵循重定向,转储头到stdout和发送数据到/dev/null(这是一个GET,不是POST,但你可以做同样的事情与POST -只是添加任何选项,你已经在使用的POST数据)

注意- d后面的-表示输出“文件”是标准输出。

其他回答

-D, --dump-header <file>
       Write the protocol headers to the specified file.

       This  option  is handy to use when you want to store the headers
       that a HTTP site sends to you. Cookies from  the  headers  could
       then  be  read  in  a  second  curl  invocation by using the -b,
       --cookie option! The -c, --cookie-jar option is however a better
       way to store cookies.

and

-S, --show-error
       When used with -s, --silent, it makes curl show an error message if it fails.

and

-L/--location
      (HTTP/HTTPS) If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response
      code), this option will make curl redo the request on the new place. If used together with -i/--include or -I/--head, headers from  all  requested
      pages  will  be  shown.  When authentication is used, curl only sends its credentials to the initial host. If a redirect takes curl to a different
      host, it won’t be able to intercept the user+password. See also --location-trusted on how to change this. You can limit the amount of redirects to
      follow by using the --max-redirs option.

      When curl follows a redirect and the request is not a plain GET (for example POST or PUT), it will do the following request with a GET if the HTTP
      response was 301, 302, or 303. If the response code was any other 3xx code, curl will re-send the following  request  using  the  same  unmodified
      method.

从手册页。所以

curl -sSL -D - www.acooke.org -o /dev/null

遵循重定向,转储头到stdout和发送数据到/dev/null(这是一个GET,不是POST,但你可以做同样的事情与POST -只是添加任何选项,你已经在使用的POST数据)

注意- d后面的-表示输出“文件”是标准输出。

对于长响应体(以及各种其他类似的情况),我使用的解决方案总是将管道输送到更少的地方,因此

curl -i https://api.github.com/users | less

or

curl -s -D - https://api.github.com/users | less

会完成任务的。

headcurl。CMD (windows版本)

curl -sSkv -o NUL %* 2>&1

我不想要进度条-s, 但我想要误差-S, 不用担心有效的HTTPS证书-k, 获取高冗余-v(这是关于故障排除的,对吗?), 没有输出(干净的方式)。 哦,我想转发stderr到stdout,所以我可以grep反对整个事情(因为大多数或所有输出都来自stderr) %*表示[将所有参数传递给这个脚本](嗯(https://stackoverflow.com/a/980372/444255),通常只有一个参数:您正在测试的url

实际示例(关于代理问题的故障排除):

C:\depot>headcurl google.ch | grep -i -e http -e cache
Hostname was NOT found in DNS cache
GET HTTP://google.ch/ HTTP/1.1
HTTP/1.1 301 Moved Permanently
Location: http://www.google.ch/
Cache-Control: public, max-age=2592000
X-Cache: HIT from company.somewhere.ch
X-Cache-Lookup: HIT from company.somewhere.ch:1234

Linux版本

对于.bash_aliases / .bash_rc:

alias headcurl='curl -sSkv -o /dev/null $@  2>&1'

下面的命令显示额外的信息

curl -X POST http://httpbin.org/post -v > /dev/null

你可以要求服务器只发送HEAD,而不是完整的响应

curl -X HEAD -I http://httpbin.org/

注意:在某些情况下,服务器可能会为POST和HEAD发送不同的报头。但在几乎所有情况下,头都是相同的。

- d, dump-header 将协议头写入指定文件。

   This  option  is handy to use when you want to store the headers
   that a HTTP site sends to you. Cookies from  the  headers  could
   then  be  read  in  a  second  curl  invocation by using the -b,
   --cookie option! The -c, --cookie-jar option is however a better
   way to store cookies.