使用 cURL 仅从 HTTP POST 获取响应标头 [英] Getting only response header from HTTP POST using cURL
问题描述
只能使用 HTTP HEAD 请求标头,作为 curl(1)
中的选项 -I
.
One can request only the headers using HTTP HEAD, as option -I
in curl(1)
.
$ curl -I /
冗长的 HTML 响应正文很难在命令行中使用,因此我只想获取标头作为我的 POST 请求的反馈.但是,HEAD 和 POST 是两种不同的方法.
Lengthy HTML response bodies are a pain to get in command-line, so I'd like to get only the header as feedback for my POST requests. However, HEAD and POST are two different methods.
如何让 cURL 仅显示 POST 请求的响应标头?
How do I get cURL to display only response headers to a POST request?
推荐答案
-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 做同样的事情 - 只需添加您已经使用的任何选项发布数据)
follows redirects, dumps the headers to stdout and sends the data to /dev/null (that's a GET, not a POST, but you can do the same thing with a POST - just add whatever option you're already using for POSTing data)
注意-D
后面的-
表示输出文件"是stdout.
note the -
after the -D
which indicates that the output "file" is stdout.
这篇关于使用 cURL 仅从 HTTP POST 获取响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!