可以使用HTTP HEAD只请求标头,作为curl(1)
选项-I
。
$ curl -I /
冗长的HTML响应正文在命令行中很麻烦,因此我只想获取标头作为对POST请求的反馈。 但是,HEAD和POST是两种不同的方法。
如何获取curl以仅显示POST请求的响应标头?
#1楼
-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
跟随重定向,将标头转储到stdout并将数据发送到/ dev / null(这是GET,而不是POST,但是您可以对POST执行相同的操作-只需添加已经用于发布数据的任何选项)
注意-D
之后的-
表示输出“文件”为stdout。
#2楼
对于长响应主体(以及各种其他类似情况),我使用的解决方案始终是将管道传递给less
,因此
curl -i https://api.github.com/users | less
要么
curl -s -D - https://api.github.com/users | less
会做的工作。
#3楼
虽然其他答案并非在所有情况下都对我有用,但我可以找到的最佳解决方案(也适用于POST
),可从此处获取 :
curl -vs 'https://some-site.com' 1> /dev/null
#4楼
更加容易-这是我用来避免短链接跟踪的方式 -如下:
curl -IL http://bit.ly/in-the-shadows
…也跟随链接 。
#5楼
以下命令显示其他信息
curl -X POST http://httpbin.org/post -vvv > /dev/null
您可以要求服务器仅发送HEAD,而不发送完整响应
curl -X HEAD -I http://httpbin.org/
Note:
正确配置/编程的Web服务器将响应与发布不同的响应,因为它是HEAD请求而不是POST。 但是大多数时候都可以
#6楼
其他答案需要下载响应正文。 但是有一种方法可以发出一个仅获取标头的POST请求:
curl -s -I -X POST http://www.google.com
-I
本身执行HEAD请求,该请求可以被-X POST
覆盖以执行POST(或任何其他)请求,并且仍然仅获取标头数据。
#7楼
也许这有点极端,但是我使用的是这个超短版本:
curl -svo. <URL>
说明:
-v
打印调试信息(不包括标题)
-o.
发送网页数据(我们要忽略)到某个文件.
在这种情况下,它是一个目录,并且是无效的目标,并使输出被忽略。
-s
没有进度条,没有错误信息(否则,您将看到Warning: Failed to create the file .: Is a directory
)
警告:结果总是失败(就错误代码而言,无论是否可达)。 请勿在shell脚本中使用条件语句...
#8楼
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'