使用curl从HTTP POST获取响应头

Jon*_*ard 518 post curl http-headers

人们可以仅请求使用HTTP HEAD报头,作为选项-Icurl(1).

$ curl -I /
Run Code Online (Sandbox Code Playgroud)

冗长的HTML响应主体很难进入命令行,因此我只想获得标题作为我的POST请求的反馈.但是,HEAD和POST是两种不同的方法.

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

and*_*oke 721

-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.
Run Code Online (Sandbox Code Playgroud)

-S, --show-error
       When used with -s, --silent, it makes curl show an error message if it fails.
Run Code Online (Sandbox Code Playgroud)

-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.
Run Code Online (Sandbox Code Playgroud)

从手册页.所以

curl -sSL -D - www.acooke.org -o /dev/null
Run Code Online (Sandbox Code Playgroud)

跟随重定向,将标头转储到stdout并将数据发送到/ dev/null(这是一个GET,而不是POST,但你可以用POST做同样的事情 - 只需添加你已用于POST数据的任何选项)

注意-后面的-D内容表示输出"文件"是标准输出.

  • 在Windows上的curl中,你可以这样做:`curl -s -D - http://yahoo.com -o $ null` (36认同)
  • 如果您正在使用PowerShell,则上述注释有效.对于cmd.exe,请使用`curl -s -D - http://yahoo.com -o nul` (21认同)
  • URL前面的" - "可能看起来不重要,但事实并非如此. (16认同)
  • @mamachanko`-D`接受一个参数说明输出应该去哪里.单个破折号意味着它应该去stdout. (3认同)

sir*_*usa 147

其他答案需要下载响应正文.但是有一种方法可以生成一个只获取标头的POST请求:

curl -s -I -X POST http://www.google.com
Run Code Online (Sandbox Code Playgroud)

一个-I由自身执行HEAD请求可以通过重写-X POST来执行POST(或任何其他)请求,仍然只能得到标题数据.

  • 当你真的想要`POST`一些数据时,这不起作用.Curl说:`警告:你只能选择一种HTTP请求方法!您要求POST警告:( - d, - data)和HEAD(-I, - head).` (15认同)
  • 这个答案实际上是正确的,因为Web服务器可以根据请求方法返回不同的头.如果要在GET上检查标头,则必须使用GET请求. (12认同)
  • 在我看来,这是最正确的答案.它很容易记住,它实际上发送`GET`请求并且不下载整个响应体(或者至少不输出它).`-s`标志也没有必要. (5认同)
  • @nickboldt这里的要点是服务器可能对HEAD请求的响应不同于对POST或GET请求的响应(有些服务器实际上是这样做的),所以`-X HEAD`在这里不是可靠的解决方案. (2认同)

zai*_*eer 51

以下命令显示额外的信息

curl -X POST http://httpbin.org/post -vvv > /dev/null
Run Code Online (Sandbox Code Playgroud)

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

curl -X HEAD -I http://httpbin.org/
Run Code Online (Sandbox Code Playgroud)

Note:正确配置/编程的Web服务器将以不同于帖子的响应,因为它是HEAD请求而不是POST.但大部分时间都有效

  • `-X HEAD`的问题是服务器可能会以不同的方式响应,因为它现在收到一个`HEAD`请求而不是'GET`(或者之前的请求是什么) (15认同)
  • 不幸的是,另一个答案赢了,因为这是正确答案 - 它不会不必要地转移大量数据. (5认同)
  • `警告:使用-X/ - 请求将自定义HTTP方法设置为HEAD可能无法使用警告:您想要的方式.考虑使用-I/ - head代替 (4认同)
  • @bfontaine 是 [XY 问题](https://xyproblem.info/) 的完美示例 (2认同)

fia*_*jaf 50

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

curl -i https://api.github.com/users | less
Run Code Online (Sandbox Code Playgroud)

要么

curl -s -D - https://api.github.com/users | less
Run Code Online (Sandbox Code Playgroud)

会做的.

  • 这些是_不_等效的。第一个发出“HEAD”请求,许多服务器对此做出不同的响应。第二个发出一个“GET”请求,这更像是我们在这里寻找的内容。 (3认同)

exe*_*ook 18

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

curl -svo. <URL>
Run Code Online (Sandbox Code Playgroud)

说明:

-v 打印调试信息(确实包含标题)

-o.将网页数据(我们要忽略的数据)发送到某个文件,.在这种情况下,该文件是目录,并且是无效的目标,并导致输出被忽略。

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

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

  • 为什么使用“-o.”而不是“-o /dev/null”? (3认同)
  • @bfontaine还有其他答案,显示了如何以最正确的方式执行此操作,这是在这里显示基本上可以完成相同操作的简短选择。 (2认同)
  • 您应该在回答中澄清该命令_总是_失败。`卷曲-svo。&lt;url&gt; &amp;&amp; echo foo` 不会打印 `foo`,因为 `-o.` 使 `curl` 返回非零(= 错误)代码:`curl: (23) 写入正文失败`。 (2认同)
  • 以返回错误结束的“解决方案”不是有效的解决方案。这是一次幸福的意外。如果出了问题,你无法知道,因为你已经接受了错误 (2认同)

kai*_*ser 15

更容易 - 这是我用来避免短链接跟踪 - 以下内容:

curl -IL http://bit.ly/in-the-shadows
Run Code Online (Sandbox Code Playgroud)

...也跟随链接.


Dan*_*ner 13

虽然其他答案在所有情况下对我都不起作用,但我能找到的最佳解决方案(同样合作POST),取自这里:

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


Fra*_*cke 8

headcurl.cmd (windows 版本)

curl -sSkv -o NUL %* 2>&1
Run Code Online (Sandbox Code Playgroud)
  • 我不想一个进度条-s
  • 但我确实想要错误-S
  • 不关心有效的 https 证书-k
  • 变得冗长-v(这是关于故障排除,是吗?),
  • 没有输出(以干净的方式)。
  • 哦,我想将stderr转发到stdout,所以我可以对整个事情进行 grep (因为大多数或所有输出都在 stderr 中)
  • %*意味着[将所有参数传递给这个脚本](好吧(/sf/answers/68626071/),通常这只是一个参数:您正在测试的网址

实际示例(关于解决代理问题):

C:\depot>headcurl google.ch | grep -i -e http -e cache
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

Linux版本

为您的.bash_aliases/ .bash_rc

alias headcurl='curl -sSkv -o /dev/null $@  2>&1'
Run Code Online (Sandbox Code Playgroud)