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

$ curl -I /

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

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


当前回答

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

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

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

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

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

其他回答

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

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

or

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

会完成任务的。

-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 -svo. <URL>

解释:

打印调试信息(包含头文件)

- o。将网页数据(我们想要忽略)发送到某个文件。在本例中,它是一个目录,是一个无效的目的地,并使输出被忽略。

-s没有进度条,没有错误信息(否则你会看到Warning: Failed to create the file .: Is a directory)

警告:结果总是失败(就错误代码而言,无论是否可达)。不要在shell脚本中使用条件语句…

- 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.

其他答案要求下载响应体。但是有一种方法可以让POST请求只获取头文件:

curl -s -I -X POST http://www.google.com

-I本身执行HEAD请求,该请求可以被-X POST覆盖以执行POST(或任何其他)请求,并且仍然只获得头数据。