为什么curl会在输出中重复标题?

时间:2017-05-28 22:11:23

标签: unix 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

2 个答案:

答案 0 :(得分:2)

最佳猜测:使用-v告诉curl是详细的(发送调试信息)到STDERR。使用-I告诉curl将标头转储到STDOUT。默认情况下,您的shell结合了STDOUT和STDERR。将stdout和stderr分开,你就可以避免混淆。

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

答案 1 :(得分:0)

使用:

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

当使用verbose命令行标志调用cURL时,它会将详细输出发送到stderr而不是stdout。上面的命令将stderr重定向到stdout(2&gt;&amp; 1),然后我们将组合输出传递给grep并使用上面的regex只返回以*,&lt;或&gt;开头的行。输出中的所有其他行(包括您首先关注的欺骗行)都将从输出中删除。