Getting only response header from HTTP POST using curl
在
1 | $ curl -I / |
冗长的HTML响应体很难进入命令行,所以我只想得到标题作为我的post请求的反馈。然而,头和柱是两种不同的方法。
如何让curl只显示对post请求的响应头?
1 2 3 4 5 6 7 8 | -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. |
和
1 2 | -S, --show-error When used with -s, --silent, it makes curl show an error message if it fails. |
和
1 2 3 4 5 6 7 8 9 10 | -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. |
从手册页。所以
1 | curl -sSL -D - www.acooke.org -o /dev/null |
遵循重定向,将头转储到stdout并将数据发送到/dev/null(这是一个get,而不是一个post,但是您可以对post执行相同的操作-只需添加您已经用于发布数据的任何选项)
注意
其他答案需要下载响应主体。但是有一种方法可以发出一个只获取头的POST请求:
1 | curl -s -I -X POST http://www.google.com |
对于长响应体(以及各种其他类似情况),我使用的解决方案总是通过管道连接到
1 | curl -i https://api.github.com/users | less |
或
1 | curl -s -D - https://api.github.com/users | less |
会完成任务的。
以下命令显示额外信息
1 | curl -X POST http://httpbin.org/post -vvv > /dev/null |
您可以要求服务器只发送head,而不是完全响应
1 | curl -X HEAD -I http://httpbin.org/ |
正确配置/编程的
更简单-这是我用来避免短链接跟踪的功能-如下所示:
1 | curl -IL http://bit.ly/in-the-shadows |
…也遵循链接。
虽然其他答案在所有情况下对我都不起作用,但我能找到的最佳解决方案(与
也许有点极端,但我使用的是这个超短版本:
1 | curl -svo. <URL> |
说明: