可以使用HTTP HEAD只请求报头,如curl(1)中的选项-I。
$ curl -I /
在命令行中获取冗长的HTML响应体是一件痛苦的事情,因此我希望只获得POST请求的报头作为反馈。但是,HEAD和POST是两种不同的方法。
如何让cURL只显示POST请求的响应头?
可以使用HTTP HEAD只请求报头,如curl(1)中的选项-I。
$ curl -I /
在命令行中获取冗长的HTML响应体是一件痛苦的事情,因此我希望只获得POST请求的报头作为反馈。但是,HEAD和POST是两种不同的方法。
如何让cURL只显示POST请求的响应头?
当前回答
其他答案要求下载响应体。但是有一种方法可以让POST请求只获取头文件:
curl -s -I -X POST http://www.google.com
-I本身执行HEAD请求,该请求可以被-X POST覆盖以执行POST(或任何其他)请求,并且仍然只获得头数据。
其他回答
更容易-这也遵循链接。
curl -IL http://example.com/in-the-shadows
- 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.
对于长响应体(以及各种其他类似的情况),我使用的解决方案总是将管道输送到更少的地方,因此
curl -i https://api.github.com/users | less
or
curl -s -D - https://api.github.com/users | less
会完成任务的。
虽然其他答案在所有情况下都不适合我,但我能找到的最佳解决方案(也可以使用POST),从这里开始:
Curl -vs 'https://some-site.com' 1> /dev/null
-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后面的-表示输出“文件”是标准输出。