首页 文章

使用curl从HTTP POST获取响应标头

提问于
浏览
447

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

$ curl -I /

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

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

6 回答

  • 12

    更容易 - 这是我用avoid Shortlink tracking - 是以下内容:

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

    ...也跟随链接 .

  • 639

    以下命令显示额外的信息

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

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

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

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

  • 15

    虽然其他答案在所有情况下对我都不起作用,但我可以找到最佳解决方案(也可以使用 POST ),取自here

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

  • 36

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

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

    要么

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

    会做的 .

  • 108

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

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

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

  • 41
    -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 之后的 - ,表示输出"file"是stdout .

相关问题