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

kq0g1dla  于 2022-11-13  发布在  其他
关注(0)|答案(9)|浏览(216)

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

$ curl -I /

在命令行中获取冗长的HTML响应正文是一件痛苦的事情,所以我想只获取头部作为POST请求的反馈。
如何让cURL只显示POST请求的响应标头?

iibxawm4

iibxawm41#

-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执行相同的操作-只需添加您已经用于POST数据的任何选项)
请注意-D后面的-,它表示输出“文件”是stdout。

piah890a

piah890a2#

其他的答案需要下载响应主体。但是有一种方法可以生成一个POST请求,它只获取头:

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

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

fkvaft9z

fkvaft9z3#

以下命令显示额外信息

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

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

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

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

mkh04yzy

mkh04yzy4#

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

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

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

我会做的。

uidvcgyl

uidvcgyl5#

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

curl -svo. <URL>

说明:
-v打印调试信息(不包括标题)
-o.将网页数据(我们希望忽略)发送到某个文件,在本例中为.,该文件是一个目录,是一个无效的目标,并使输出被忽略。
-s没有进度条,没有错误信息(否则您将看到Warning: Failed to create the file .: Is a directory

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

l7mqbcuq6#

更容易-这也遵循链接。

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

bihw5rsg7#

虽然其他答案并不适用于所有情况,但我能找到的最佳解决方案(也适用于POST)取自here
curl -vs 'https://some-site.com' 1> /dev/null

dw1jzc5e

dw1jzc5e8#

headcurl.cmd(视窗版本)

curl -sSkv -o NUL %* 2>&1
  • 我不想要进度条-s
  • 但我确实想要错误-S
  • 不关心有效的HTTPS证书-k
  • 获得高详细度-v(这是关于故障排除的,是吗?),
  • 没有输出(以干净方式)。
  • 哦,我还想使用forward stderr to stdout,这样我就可以对整个内容使用grep(因为大多数或所有输出都是stderr格式)
  • %*的意思是[将所有参数传递给此脚本](通常是(https://stackoverflow.com/a/980372/444255),通常只有一个参数:你正在测试的网址
    实际示例(关于代理问题的疑难解答):

第一个

Linux版本

对于您的.bash_aliases/.bash_rc

alias headcurl='curl -sSkv -o /dev/null $@  2>&1'
uxh89sit

uxh89sit9#

-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.

相关问题