可以使用 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
之后的 -
,它表示输出“文件”是标准输出。
其他答案需要下载响应正文。但是有一种方法可以发出仅获取标头的 POST 请求:
curl -s -I -X POST http://www.google.com
-I
本身执行一个 HEAD 请求,该请求可以被 -X POST
覆盖以执行 POST(或任何其他)请求,并且仍然只获取标头数据。
GET
请求并且不下载整个响应体(或者至少不输出它)。 -s
标志也不是必需的。
GET
替换为 POST
,它会按预期工作。 or any other
是关键。
POST
某些数据时,这不起作用。卷毛说:Warning: You can only select one HTTP request method! You asked for both POST Warning: (-d, --data) and HEAD (-I, --head).
-X HEAD
在这里不是可靠的解决方案。
以下命令显示额外信息
curl -X POST http://httpbin.org/post -v > /dev/null
您可以要求服务器仅发送 HEAD,而不是完整响应
curl -X HEAD -I http://httpbin.org/
Note:
在某些情况下,服务器可能会为 POST 和 HEAD 发送不同的标头。但在几乎所有情况下,标题都是相同的。
-X HEAD
和 -I
完全等价。
-X HEAD
的问题是服务器可能会做出不同的响应,因为它现在接收的是 HEAD
请求而不是 GET
(或任何先前的请求)
Warning: Setting custom HTTP method to HEAD with -X/--request may not work the Warning: way you want. Consider using -I/--head instead.
对于长响应体(以及各种其他类似情况),我使用的解决方案始终是通过管道传输到 less
,所以
curl -i https://api.github.com/users | less
或者
curl -s -D - https://api.github.com/users | less
将完成这项工作。
HEAD
请求,许多服务器对它的响应不同。第二个发出 GET
请求,这更像我们在这里寻找的。
也许这有点极端,但我使用的是这个超短版本:
curl -svo. <URL>
解释:
-v
打印调试信息(包括标题)
-o.
将网页数据(我们想要忽略)发送到某个文件,在本例中为 .
,该文件是一个目录,并且是无效的目标,并且会使输出被忽略。
-s
没有进度条,没有错误信息(否则您会看到 Warning: Failed to create the file .: Is a directory
)
警告:结果总是失败(就错误代码而言,是否可达)。不要在 shell 脚本中的条件语句中使用...
-o.
而不是 -o /dev/null
?
-o.
而不是 -o /dev/null
curl -svo. <url> && echo foo
不会打印 foo
,因为 -o.
使 curl
返回一个非零(= 错误)代码:curl: (23) Failed writing body
。
容易得多 - 这也遵循链接。
curl -IL http://example.com/in-the-shadows
虽然其他答案在所有情况下都不适用于我,但我能找到的最佳解决方案(也与 POST
一起使用)取自 here:
curl -vs 'https://some-site.com' 1> /dev/null
headcurl.cmd (windows 版)
curl -sSkv -o NUL %* 2>&1
我不想要进度条-s,
但我确实想要错误-S,
不关心有效的 https 证书 -k,
越来越冗长 -v (这是关于故障排除,是吗?),
没有输出(以干净的方式)。
哦,我想将标准错误转发到标准输出,所以我可以对整个事情进行 grep(因为大多数或所有输出都来自标准错误)
%* 表示[将所有参数传递给此脚本](嗯(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'
-X POST
添加到传递参数,如果您想要 GET,请使用 GET(即默认值),因为响应可能会有所不同。 - 除非你在生产脚本中做大量的卷曲(不是为了诊断和开发)我不关心一点带宽。
curl -s -D - http://yahoo.com -o nul
-D
接受一个参数,说明输出应该去哪里。单破折号意味着它应该去标准输出。