2017-05-28 62 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文件) –