Jon*_*ard 518 post curl http-headers
人们可以仅请求使用HTTP HEAD报头,作为选项-I
在curl(1)
.
$ curl -I /
Run Code Online (Sandbox Code Playgroud)
冗长的HTML响应主体很难进入命令行,因此我只想获得标题作为我的POST请求的反馈.但是,HEAD和POST是两种不同的方法.
如何让curl仅显示POST请求的响应头?
and*_*oke 721
-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.
Run Code Online (Sandbox Code Playgroud)
和
-S, --show-error
When used with -s, --silent, it makes curl show an error message if it fails.
Run Code Online (Sandbox Code Playgroud)
和
-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.
Run Code Online (Sandbox Code Playgroud)
从手册页.所以
curl -sSL -D - www.acooke.org -o /dev/null
Run Code Online (Sandbox Code Playgroud)
跟随重定向,将标头转储到stdout并将数据发送到/ dev/null(这是一个GET,而不是POST,但你可以用POST做同样的事情 - 只需添加你已用于POST数据的任何选项)
注意-
后面的-D
内容表示输出"文件"是标准输出.
sir*_*usa 147
其他答案需要下载响应正文.但是有一种方法可以生成一个只获取标头的POST请求:
curl -s -I -X POST http://www.google.com
Run Code Online (Sandbox Code Playgroud)
一个-I
由自身执行HEAD请求可以通过重写-X POST
来执行POST(或任何其他)请求,仍然只能得到标题数据.
zai*_*eer 51
以下命令显示额外的信息
curl -X POST http://httpbin.org/post -vvv > /dev/null
Run Code Online (Sandbox Code Playgroud)
您可以要求服务器仅发送HEAD,而不是完整响应
curl -X HEAD -I http://httpbin.org/
Run Code Online (Sandbox Code Playgroud)
Note:
正确配置/编程的Web服务器将以不同于帖子的响应,因为它是HEAD请求而不是POST.但大部分时间都有效
fia*_*jaf 50
对于长响应体(以及其他各种类似的情况),我使用的解决方案始终是管道less
,所以
curl -i https://api.github.com/users | less
Run Code Online (Sandbox Code Playgroud)
要么
curl -s -D - https://api.github.com/users | less
Run Code Online (Sandbox Code Playgroud)
会做的.
exe*_*ook 18
也许这有点极端,但是我使用的是这个超短版本:
curl -svo. <URL>
Run Code Online (Sandbox Code Playgroud)
说明:
-v
打印调试信息(确实包含标题)
-o.
将网页数据(我们要忽略的数据)发送到某个文件,.
在这种情况下,该文件是目录,并且是无效的目标,并导致输出被忽略。
-s
没有进度条,没有错误信息(否则您将看到Warning: Failed to create the file .: Is a directory
)
警告:结果总是失败(就错误代码而言,无论是否可达)。请勿在shell脚本中使用条件语句...
kai*_*ser 15
更容易 - 这是我用来避免短链接跟踪 - 以下内容:
curl -IL http://bit.ly/in-the-shadows
Run Code Online (Sandbox Code Playgroud)
...也跟随链接.
headcurl.cmd (windows 版本)
curl -sSkv -o NUL %* 2>&1
Run Code Online (Sandbox Code Playgroud)
-s
,-S
,-k
,-v
(这是关于故障排除,是吗?),%*
意味着[将所有参数传递给这个脚本](好吧(/sf/answers/68626071/),通常这只是一个参数:您正在测试的网址实际示例(关于解决代理问题):
C:\depot>headcurl google.ch | grep -i -e http -e cache
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
Linux版本
为您的.bash_aliases
/ .bash_rc
:
alias headcurl='curl -sSkv -o /dev/null $@ 2>&1'
Run Code Online (Sandbox Code Playgroud)