使用curl从HTTP POST获取响应头

时间:2012-04-08 03:12:04

标签: post curl http-headers

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

$ curl -I /

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

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

8 个答案:

答案 0 :(得分:680)

-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,表示输出“文件”是标准输出。

答案 1 :(得分:125)

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

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

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

答案 2 :(得分:44)

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

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

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

将完成这项工作。

答案 3 :(得分:44)

以下命令显示额外信息

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

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

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

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

答案 4 :(得分:15)

更容易 - 这就是我对avoid Shortlink tracking的使用 - 以下是:

curl -IL http://bit.ly/in-the-shadows

...也跟随链接

答案 5 :(得分:13)

虽然其他答案在所有情况下对我都不起作用,但我可以找到最佳解决方案(同时使用{ "message_id": "123", "message": "Hello World" } ),取自here

POST

答案 6 :(得分:6)

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

curl -svo. <URL>

说明:

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

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

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

答案 7 :(得分:1)

headcurl.cmd (Windows版本)

curl -sSkv -o NUL %* 2>&1
  • 我不需要进度栏-s
  • 但是我确实想要错误-S
  • 不必担心有效的https证书-k
  • 获得较高的详细程度-v(这是有关故障排除的吗?),
  • 无输出(以干净的方式)。
  • 哦,我想forward stderr to stdout,所以我可以对整个内容进行grep(因为大多数或所有输出都来自stderr)
  • %*的意思是[将所有参数传递给该脚本](well(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'
相关问题