当前位置: 首页 > 知识库问答 >
问题:

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

傅正阳
2023-03-14

您只能使用HTTP HEAD请求报头,如curl(1)中的选项-i

$ curl -I /

冗长的HTML响应体在命令行中很难得到,所以我希望只得到标题作为POST请求的反馈。但是,头和岗是两种不同的方法。

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

共有2个答案

邴星洲
2023-03-14

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

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

-i本身执行HEAD请求,该请求可以被-xpost覆盖,以执行POST(或任何其他)请求,并且仍然只获得header数据。

周睿范
2023-03-14
-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后面的-表示输出“file”是stdout。

 类似资料:
  • 问题内容: 好的,因此我可以使用访问HTTP ajax响应标头 但似乎没有日期,尽管它在那里: 而代码只显示了这一点: 这是ajax调用: 有没有办法可以在响应头中获取日期? 问题答案: 这有帮助:

  • 问题内容: 我正在使用OkHttp客户端和Jackson的Retrofit进行Json序列化,并想获取响应的标头。 我知道我可以扩展OkClient并拦截它。但这是在反序列化过程开始之前进行的。 我基本上需要的是将标头与反序列化的Json Object一起使用。 问题答案: 在Retrofit 1.9.0中,如果您使用接口的回调异步版本, 然后您的回调将收到一个对象 其中有一个方法叫做 对于Ret

  • 问题内容: 所以这是我的问题,我正在使用AJAX(jQuery)将表单发布到,但是页面实际上需要回显诸如或的响应。我不确定如何从中获取响应并将其存储为变量… 这是我到目前为止的代码: 我还需要在json中回显响应吗?还是纯文本会好? 抱歉,这听起来像是一个愚蠢的问题,这是我第一次在Ajax中进行此类操作。 PS:如何在上述代码中命名POST请求? 问题答案: 实际上,这是服务器上所需的全部内容。

  • 我想问一下在执行查询时如何从PostgreSQL获得消息响应。 来自PostgreSQL的消息响应我的意思是这样的,例如: null

  • 问题内容: 我在服务器上调用PHP cURL方法,响应为XML类型。cURL将输出(在删除标记之后)保存在标量类型变量中。有没有一种方法可以将其存储在对象/哈希/数组中,以便于解析? 问题答案:

  • 问题内容: 我想拥有一个独立的PHP类,在这里我想拥有一个通过cURL调用API并获取响应的函数。有人可以帮我吗? 谢谢。 问题答案: 只需使用下面的代码即可获得来自宁静的Web服务URL的响应,而我使用的是社交提及URL。