2017-05-28 41 views
0

选项我使用:为什么curl会在输出中重复标题?

-I, --head 
     (HTTP/FTP/FILE) Fetch the HTTP-header only! HTTP-servers feature 
     the command HEAD which this uses to get nothing but the header 
     of a document. When used on an FTP or FILE file, curl displays 
     the file size and last modification time only. 

-L, --location 
     (HTTP/HTTPS) If the server reports that the requested page has moved to a different location (indi- 
     cated 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. 

     You can tell curl to not change the non-GET request method to GET after a 30x response by using the 
     dedicated options for that: --post301, --post302 and -post303. 

-v, --verbose 
     Be more verbose/talkative during the operation. Useful for debugging and seeing what's going on 
     "under the hood". A line starting with '>' means "header data" sent by curl, '<' means "header data" 
     received by curl that is hidden in normal cases, and a line starting with '*' means additional info 
     provided by curl. 

     Note that if you only want HTTP headers in the output, -i, --include might be the option you're 
     looking for. 

     If you think this option still doesn't give you enough details, consider using --trace or --trace- 
     ascii instead. 

     This option overrides previous uses of --trace-ascii or --trace. 

     Use -s, --silent to make curl quiet. 

下面是我想了解一下输出。在包含重定向(301)的响应中,所有标题都会显示两次,但只有其中一个重复项的前面有<。我该如何解释这一点?

$ curl -ILv http://www.mail.com 

* Rebuilt URL to: http://www.mail.com/ 
* Trying 74.208.122.4... 
* Connected to www.mail.com (74.208.122.4) port 80 (#0) 
> HEAD/HTTP/1.1 
> Host: www.mail.com 
> User-Agent: curl/7.43.0 
> Accept: */* 
> 
< HTTP/1.1 301 Moved Permanently 
HTTP/1.1 301 Moved Permanently 
< Date: Sun, 28 May 2017 22:02:16 GMT 
Date: Sun, 28 May 2017 22:02:16 GMT 
< Server: Apache 
Server: Apache 
< Location: https://www.mail.com/ 
Location: https://www.mail.com/ 
< Vary: Accept-Encoding 
Vary: Accept-Encoding 
< Connection: close 
Connection: close 
< Content-Type: text/html; charset=iso-8859-1 
Content-Type: text/html; charset=iso-8859-1 

< 
* Closing connection 0 
* Issue another request to this URL: 'https://www.mail.com/' 
* Trying 74.208.122.4... 
* Connected to www.mail.com (74.208.122.4) port 443 (#1) 
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 
* Server certificate: *.mail.com 
* Server certificate: thawte SSL CA - G2 
* Server certificate: thawte Primary Root CA 
> HEAD/HTTP/1.1 
> Host: www.mail.com 
> User-Agent: curl/7.43.0 
> Accept: */* 
> 
< HTTP/1.1 200 OK 
HTTP/1.1 200 OK 
< Date: Sun, 28 May 2017 22:02:16 GMT 
Date: Sun, 28 May 2017 22:02:16 GMT 
< Server: Apache 
Server: Apache 
< Vary: X-Forwarded-Proto,Host,Accept-Encoding 
Vary: X-Forwarded-Proto,Host,Accept-Encoding 
< Set-Cookie: cookieKID=kid%40autoref%40mail.com; Domain=.mail.com; Expires=Tue, 27-Jun-2017 22:02:16 GMT; Path=/ 
Set-Cookie: cookieKID=kid%40autoref%40mail.com; Domain=.mail.com; Expires=Tue, 27-Jun-2017 22:02:16 GMT; Path=/ 
< Set-Cookie: cookiePartner=kid%40autoref%40mail.com; Domain=.mail.com; Expires=Tue, 27-Jun-2017 22:02:16 GMT; Path=/ 
Set-Cookie: cookiePartner=kid%40autoref%40mail.com; Domain=.mail.com; Expires=Tue, 27-Jun-2017 22:02:16 GMT; Path=/ 
< Cache-Control: no-cache, no-store, must-revalidate 
Cache-Control: no-cache, no-store, must-revalidate 
< Pragma: no-cache 
Pragma: no-cache 
< Expires: Thu, 01 Jan 1970 00:00:00 GMT 
Expires: Thu, 01 Jan 1970 00:00:00 GMT 
< Set-Cookie: JSESSIONID=F0BEF03C92839D69057FFB57C7FAA789; Path=/mailcom-webapp/; HttpOnly 
Set-Cookie: JSESSIONID=F0BEF03C92839D69057FFB57C7FAA789; Path=/mailcom-webapp/; HttpOnly 
< Content-Language: en-US 
Content-Language: en-US 
< Content-Length: 85237 
Content-Length: 85237 
< Connection: close 
Connection: close 
< Content-Type: text/html;charset=UTF-8 
Content-Type: text/html;charset=UTF-8 

< 
* Closing connection 1 

回答

1

最好的猜测:用-v你告诉curl是详细的(发送调试信息)给STDERR。与 - 我告诉curl将标题转储到标准输出。并且默认情况下,您的shell将STDOUT和STDERR组合在一起。单独的stdout和stderr,你会避免混淆。

curl -ILv http://www.mail.com >stdout.log 2>stderr.log ; cat stdout.log

0

使用:

curl -ILv http://www.mail.com 2>&1 | grep '^[<>\*].*$' 

当卷曲时调用冗长命令行标记时,它发送详细输出到stderr代替标准输出。上面的命令将stderr重定向到标准输出(2> & 1),然后我们将组合输出传输到grep,并使用上述正则表达式仅返回以*,<或>开头的行。输出中的所有其他行(包括您首先关心的模糊)都将从输出中删除。

+0

与省略了'-v'选项的问题是,袅袅不显示的要求 - 它只是显示的响应。我也需要看到这些请求。 – 7stud

+0

请参阅上面对我原始答案的编辑。这应该让你到你想去的地方。 cURL -v将输出发送到stderr并不明显,所以我只是将stderr重定向到stdout,然后使用grep来过滤输出。如果您需要额外的线路输出,您可以随时调整正则表达式。 –

+0

顺便说一句,hanshenrik的答案很不错,但你需要输出到日志文件,然后处理。我上面提供的单行命令可以让您直接输出所需的输出信息,这样您就可以避免打开日志文件(也可以让您同时运行多个请求,而无需通过并发运行来执行stdout.log文件) –