使用curl从HTTP POST获取响应头

可以使用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.

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

-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

遵循重定向,将头文件转储到标准输出并将数据发送到/ dev / null(这是一个GET,而不是POST,但是您可以使用POST完成相同的操作 - 只需添加您已用于POST数据的任何选项)

注意--D ,表示输出“文件”是stdout。


其他答案要求下载响应主体。 但是有一种方法可以创建POST请求,只会获取标题:

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

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


对于长时间响应的机构(以及其他各种类似的情况),我使用的解决方案总是将管道less ,所以

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

要么

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

将完成这项工作。

链接地址: http://www.djcxy.com/p/7093.html

上一篇: Getting only response header from HTTP POST using curl

下一篇: How does HTTP file upload work?