How to display request headers with command line curl

后端 未结 9 628
一整个雨季
一整个雨季 2020-11-28 17:19

Command line curl can display response header by using -D option, but I want to see what request header it is sending. How can I do that?

相关标签:
9条回答
  • 2020-11-28 17:51

    curl's -v or --verbose option shows the HTTP request headers, among other things. Here is some sample output:

    $ 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
    
    0 讨论(0)
  • 2020-11-28 17:53

    I believe the command line switch you are looking for to pass to curl is -I.

    Example usage:

    $ 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
    

    Additionally, if you encounter a response HTTP status code of 301, you might like to also pass a -L argument switch to tell curl to follow URL redirects, and, in this case, print the headers of all pages (including the URL redirects), illustrated below:

    $ 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
    
    0 讨论(0)
  • 2020-11-28 17:56

    You get a nice header output with the following command:

     curl -L -v -s -o /dev/null google.de
    
    • -L, --location follow redirects
    • -v, --verbose more output, indicates the direction
    • -s, --silent don't show a progress bar
    • -o, --output /dev/null don't show received body

    Or the shorter version:

     curl -Lvso /dev/null google.de
    

    Results in:

    * 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
    

    As you can see curl outputs both the outgoing and the incoming headers and skips the bodydata althought telling you how big the body is.

    Additionally for every line the direction is indicated so that it is easy to read. I found it particular useful to trace down long chains of redirects.

    0 讨论(0)
  • 2020-11-28 17:58

    A popular answer for displaying response headers, but OP asked about request headers.

    curl -s -D - -o /dev/null http://example.com
    
    • -s : Avoid showing progress bar
    • -D - : Dump headers to a file, but - sends it to stdout
    • -o /dev/null : Ignore response body

    This is better than -I as it doesn't send a HEAD request, which can produce different results.

    It's better than -v because you don't need so many hacks to un-verbose it.

    0 讨论(0)
  • 2020-11-28 18:01

    the -v option for curl is too verbose in the error output which contains the leading *(status line) or >(request head field) or <(response head field). to get only the request head field:

    curl -v -sS www.stackoverflow.com 2>&1 >/dev/null | grep '>' | cut -c1-2 --complement
    

    to get only the request head field:

    curl -v -sS www.stackoverflow.com 2>&1 >/dev/null | grep '<' | cut -c1-2 --complement
    

    or to dump it into /tmp/test.txt file with the -D option

    curl -D /tmp/test.txt -sS www.stackoverflow.com > /dev/null
    

    in order to filter the -v output, you should direct the error output to terminal and the std output to /dev/null, the -s option is to forbid the progress metering

    0 讨论(0)
  • 2020-11-28 18:03

    The verbose option is handy, but if you want to see everything that curl does (including the HTTP body that is transmitted, and not just the headers), I suggest using one of the below options:

    • --trace-ascii - # stdout
    • --trace-ascii output_file.txt # file
    0 讨论(0)
提交回复
热议问题