命令行curl可以使用-D
选项显示响应头,但我想查看它发送的请求头。我怎么能这样做?
答案 0 :(得分:667)
curl的-v
或--verbose
选项显示HTTP请求标头等。以下是一些示例输出:
$ curl -v http://google.com/
* About to connect() to google.com port 80 (#0)
* Trying 66.102.7.104... connected
* Connected to google.com (66.102.7.104) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.16.4 (i386-apple-darwin9.0) libcurl/7.16.4 OpenSSL/0.9.7l zlib/1.2.3
> Host: google.com
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Location: http://www.google.com/
< Content-Type: text/html; charset=UTF-8
< Date: Thu, 15 Jul 2010 06:06:52 GMT
< Expires: Sat, 14 Aug 2010 06:06:52 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 219
< X-XSS-Protection: 1; mode=block
<
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
* Connection #0 to host google.com left intact
* Closing connection #0
答案 1 :(得分:143)
显示响应标题的常用答案,但OP询问请求标题。
curl -sD - -o /dev/null http://example.com
-s
- 避免显示进度条-D -
- 将标头转储到文件,但-
将其发送到stdout -o /dev/null
- 忽略回复正文这比-I
更好,因为它不会发送HEAD
请求,这会产生不同的结果。
这比-v
更好,因为你不需要那么多黑客来解开它。
答案 2 :(得分:91)
我相信你想要传递给curl的命令行开关是-I
。
使用示例:
$ curl -I http://heatmiser.counterhack.com/zone-5-15614E3A-CEA7-4A28-A85A-D688CC418287
HTTP/1.1 301 Moved Permanently
Date: Sat, 29 Dec 2012 15:22:05 GMT
Server: Apache
Location: http://heatmiser.counterhack.com/zone-5-15614E3A-CEA7-4A28-A85A-D688CC418287/
Content-Type: text/html; charset=iso-8859-1
此外,如果您遇到响应HTTP状态代码301,您可能还希望传递-L
参数切换以告诉curl
遵循URL重定向,并且在这种情况下,打印所有页面的标题(包括URL重定向),如下所示:
$ curl -I -L http://heatmiser.counterhack.com/zone-5-15614E3A-CEA7-4A28-A85A-D688CC418287
HTTP/1.1 301 Moved Permanently
Date: Sat, 29 Dec 2012 15:22:13 GMT
Server: Apache
Location: http://heatmiser.counterhack.com/zone-5-15614E3A-CEA7-4A28-A85A-D688CC418287/
Content-Type: text/html; charset=iso-8859-1
HTTP/1.1 302 Found
Date: Sat, 29 Dec 2012 15:22:13 GMT
Server: Apache
Set-Cookie: UID=b8c37e33defde51cf91e1e03e51657da
Location: noaccess.php
Content-Type: text/html
HTTP/1.1 200 OK
Date: Sat, 29 Dec 2012 15:22:13 GMT
Server: Apache
Content-Type: text/html
答案 3 :(得分:69)
详细选项很方便,但是如果你想看到卷曲的所有(包括传输的HTTP主体,而不仅仅是标题),我建议使用以下选项之一:
--trace-ascii -
#stdout --trace-ascii output_file.txt
#file 答案 4 :(得分:47)
使用以下命令获得一个很好的标头输出:
curl -L -v -s -o /dev/null google.de
-L, --location
关注重定向-v, --verbose
更多输出,表示方向-s, --silent
不显示进度条-o, --output /dev/null
不显示收到的遗体或更短的版本:
curl -Lvso /dev/null google.de
结果:
* Rebuilt URL to: google.de/
* Trying 2a00:1450:4008:802::2003...
* Connected to google.de (2a00:1450:4008:802::2003) port 80 (#0)
> GET / HTTP/1.1
> Host: google.de
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Location: http://www.google.de/
< Content-Type: text/html; charset=UTF-8
< Date: Fri, 12 Aug 2016 15:45:36 GMT
< Expires: Sun, 11 Sep 2016 15:45:36 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 218
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
<
* Ignoring the response-body
{ [218 bytes data]
* Connection #0 to host google.de left intact
* Issue another request to this URL: 'http://www.google.de/'
* Trying 2a00:1450:4008:800::2003...
* Connected to www.google.de (2a00:1450:4008:800::2003) port 80 (#1)
> GET / HTTP/1.1
> Host: www.google.de
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 12 Aug 2016 15:45:36 GMT
< Expires: -1
< Cache-Control: private, max-age=0
< Content-Type: text/html; charset=ISO-8859-1
< P3P: CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."
< Server: gws
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< Set-Cookie: NID=84=Z0WT_INFoDbf_0FIe_uHqzL9mf3DMSQs0mHyTEDAQOGY2sOrQaKVgN2domEw8frXvo4I3x3QVLqCH340HME3t1-6gNu8R-ArecuaneSURXNxSXYMhW2kBIE8Duty-_w7; expires=Sat, 11-Feb-2017 15:45:36 GMT; path=/; domain=.google.de; HttpOnly
< Accept-Ranges: none
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
<
{ [11080 bytes data]
* Connection #1 to host www.google.de left intact
正如你所看到的那样,curl
输出传出和传入的标题,然后跳过了bodydata,告诉你身体有多大。
此外,每条线都指示方向,以便于阅读。我发现追踪长链重定向特别有用。
答案 5 :(得分:6)
在调试Web应用程序时,我必须自己克服这个问题。 -v
很棒,但有点太对我的口味很啰嗦。这是我提出的(仅限bash)解决方案:
curl -v http://example.com/ 2> >(sed '/^*/d')
这是有效的,因为-v
的输出被发送到stderr,而不是stdout。通过将其重定向到子shell,我们可以sed
删除以*
开头的行。由于实际输出不通过子shell,因此不会受到影响。使用子shell有点笨拙,但这是将stderr重定向到另一个命令的最简单方法。 (正如我所说,我只是用它进行测试,所以它对我来说很好。)
答案 6 :(得分:3)
curl的-v选项在错误输出中过于冗长,其中包含前导*
(状态行)或>
(请求头字段)或<
(响应头字段) )。只获取请求头字段:
curl -v -sS www.stackoverflow.com 2>&1 >/dev/null | grep '>' | cut -c1-2 --complement
仅获取请求头字段:
curl -v -sS www.stackoverflow.com 2>&1 >/dev/null | grep '<' | cut -c1-2 --complement
或使用-D选项
将其转储到/tmp/test.txt
文件中
curl -D /tmp/test.txt -sS www.stackoverflow.com > /dev/null
为了过滤-v
输出,你应该将错误输出定向到终端并将std输出定向到/ dev / null,-s选项是禁止进度计量
答案 7 :(得分:2)
类似于以下命令的命令将显示三个部分:请求标头,响应标头和数据(由CRLF分隔)。它避免了卷曲所带来的技术信息和语法噪音。
@echo off
setLocal enableDelayedExpansion
set filename=CARDSHIPPROP.csv
set originalText1=
set "replacedText1=XKXD"
for /f "tokens=*" %%a in ('type %filename%') do (
set "line=%%a"
if defined line (
call set "line=%%line:%originalText1%=%replacedText1%%%"
echo !line!>> CARDSHIPPROP_New.csv
) else (
echo.
)
该命令将产生以下输出:
curl -vs www.stackoverflow.com 2>&1 | sed '/^* /d; /bytes data]$/d; s/> //; s/< //'
说明:
GET / HTTP/1.1
Host: www.stackoverflow.com
User-Agent: curl/7.54.0
Accept: */*
HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=UTF-8
Location: https://stackoverflow.com/
Content-Length: 149
Accept-Ranges: bytes
Date: Wed, 16 Jan 2019 20:28:56 GMT
Via: 1.1 varnish
Connection: keep-alive
X-Served-By: cache-bma1622-BMA
X-Cache: MISS
X-Cache-Hits: 0
X-Timer: S1547670537.588756,VS0,VE105
Vary: Fastly-SSL
X-DNS-Prefetch-Control: off
Set-Cookie: prov=e4b211f7-ae13-dad3-9720-167742a5dff8; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="https://stackoverflow.com/">here</a></body>
-添加标题(-v),但删除进度栏(-s)-vs
-将stdout和stderr合并为单个stdout 2>&1
-使用以下命令编辑curl产生的响应sed
-删除以'*'开头的行(技术信息)/^* /d
-删除以“字节数据]”结尾的行(技术信息)/bytes data]$/d
-删除'>'前缀s/> //
-删除'<'前缀答案 8 :(得分:-1)
如果您想要更多替代方案,可以尝试安装一个现代命令行HTTP客户端,如httpie,它可用于大多数操作系统,包含brew,apt-get,pip,yum等包管理器
例如: - 对于OSX
brew install httpie
然后你可以在命令行上use使用各种选项
http GET https://www.google.com
答案 9 :(得分:-18)
设置卷曲选项
CURLINFO_HEADER_OUT =&gt;真,
CURLOPT_HEADER => 1,
并且可能将头部结果转储到mysql中...以便于记录。
如果使用浏览器比F12 / Firebug提供的更详细,也可以使用wireshark