ChatGPT解决这个技术问题 Extra ChatGPT

使用 cURL 仅从 HTTP POST 获取响应标头

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

$ curl -I /

冗长的 HTML 响应正文很难在命令行中获取,因此我只想获取标头作为我的 POST 请求的反馈。但是,HEAD 和 POST 是两种不同的方法。

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


a
anapsix
-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 之后的 -,它表示输出“文件”是标准输出。


如果您使用的是 powershell,上述评论是有效的。对于 cmd.exe 使用 curl -s -D - http://yahoo.com -o nul
@JJS 对我来说 $null 在 Win7 上工作。是不是因为在 Windows 上安装了 cLink。
URL 前面的“-”可能看起来并不重要,但事实并非如此。
@WahidSadik 为什么特别是这种情况?单杠的作用是什么?
@mamachanko -D 接受一个参数,说明输出应该去哪里。单破折号意味着它应该去标准输出。
s
siracusa

其他答案需要下载响应正文。但是有一种方法可以发出仅获取标头的 POST 请求:

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

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


这个答案实际上是正确的,因为 Web 服务器可以根据请求方法返回不同的标头。如果要检查 GET 上的标头,则必须使用 GET 请求。
在我看来,这是最正确的答案。很容易记住,它实际上发送了 GET 请求并且不下载整个响应体(或者至少不输出它)。 -s 标志也不是必需的。
@JeffPuckettII 我会说有点吹毛求疵。您可以在上述命令中将 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).
@nickboldt 这里的要点是,服务器对 HEAD 请求的响应可能与对 POST 或 GET 请求的响应不同(有些服务器实际上是这样做的),因此 -X HEAD 在这里不是可靠的解决方案。
z
zainengineer

以下命令显示额外信息

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

您可以要求服务器仅发送 HEAD,而不是完整响应

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

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


不幸的是,另一个答案赢了,因为这是正确答案——它不会不必要地传输大量数据。
@dmd 如果我正确理解 -X, --request 的 cURL 手册,-X HEAD 仍会产生“大量数据”,但 -I, --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.
f
fiatjaf

对于长响应体(以及各种其他类似情况),我使用的解决方案始终是通过管道传输到 less,所以

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

或者

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

将完成这项工作。


这些是等价的。第一个发出 HEAD 请求,许多服务器对它的响应不同。第二个发出 GET 请求,这更像我们在这里寻找的。
F
Frank Nocke

也许这有点极端,但我使用的是这个超短版本:

curl -svo. <URL>

解释:

-v 打印调试信息(包括标题)

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

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

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


为什么使用 -o. 而不是 -o /dev/null
为简洁起见,使用 @bfontaine -o. 而不是 -o /dev/null
它没有相同的行为,因此仅使用它来保存 8 个字符很奇怪。
@bfontaine 还有其他答案显示了如何以最正确的方式做到这一点,这个是为了展示基本上做同样事情的简短替代方案。
您应该在回答中说明此命令 always 失败。 curl -svo. <url> && echo foo 不会打印 foo,因为 -o. 使 curl 返回一个非零(= 错误)代码:curl: (23) Failed writing body
k
kaiser

容易得多 - 这也遵循链接。

curl -IL http://example.com/in-the-shadows

D
Daniel A. R. Werner

虽然其他答案在所有情况下都不适用于我,但我能找到的最佳解决方案(也与 POST 一起使用)取自 here

curl -vs 'https://some-site.com' 1> /dev/null


我必须将网址放在引号之间才能使其正常工作。
这是否必要可能取决于 url 和使用的 shell。我相应地改进了答案。谢谢。
F
Frank Nocke

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'

这将下载正文并消耗带宽和时间。 @siracusa 的答案 (stackoverflow.com/a/38679650/6168139) 没有这个开销。
如果&当您想要 POST 时,将 -X POST 添加到传递参数,如果您想要 GET,请使用 GET(即默认值),因为响应可能会有所不同。 - 除非你在生产脚本中做大量的卷曲(不是为了诊断和开发)我不关心一点带宽。
我计划查看服务器上的文件是否已更新或未使用“Last-Modified”。文件本身很大,有些以 GB 为单位,我通常在蜂窝互联网上。所以,这么大的带宽对我来说是个问题。
那将是hacky。我不需要这样做,因为 Siracusa 的 answer 可以准确地执行任务。