Home / Show the headers only for a request with cURL

Show the headers only for a request with cURL

cURL is an extremely useful command line tool for making HTTP requests and can be used for diagnosing errors, downloading content and so. But what if you just want to see the response headers to see, for example, if a page is doing the right sort of redirect?

tl;dr

To do a HEAD request and show the headers:

curl -I [url]

To do a GET request and show the headers only:

curl -sD - -o /dev/null https://electrictoolbox.com/

Using the -v verbose flag:

curl -s -v -o /dev/null https://electrictoolbox.com/

Using the -I or –head arguments to do a HEAD request

The simplest solution is to use -I (capital i) or –head to do a HEAD request like so:

$ curl -I https://electrictoolbox.com/
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Sun, 23 Aug 2015 21:31:49 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
$ curl -I https://electrictoolbox.com/
HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.3
Date: Sun, 23 Aug 2015 21:31:53 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: https://electrictoolbox.com/

The first example is for a page that returns content with a 200 HTTP response code, and the second a 301 redirect and the location to redirect to.

How to get the headers for a regular GET request

Th -I flag is useful, but sometimes web pages return a different response depending if you make a regular GET request compared with a HEAD request.

Calling curl with the URL on its own will dump the HTML to the command line (curl https://electrictoolbox.com/) and calling it with the -o flag allows you to dump the HTML to a file instead (curl -o /tmp/index.html https://electrictoolbox.com/).

Using the -D or –dump-header will dump the headers to a file, but you can pass – as the filename to dump them to the comment line. You can then use -o /dev/null to not show the HTML, or alternatively an actual filename to save the HTML to if you want.

And finally, when dumping the HTML to a file, curl shows a progress bar, which can be supressed with the -s flag.

Putting this all together, this command will show the headers only: 

curl -s -D - -o /dev/null https://electrictoolbox.com/

And you can combine the -s & -D flags together like this:

curl -sD - -o /dev/null https://electrictoolbox.com/

After all, here’s the response:

HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Sun, 23 Aug 2015 21:43:58 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding

Using the verbose flag instead

And a final option is to substitute the -v flag for -D, which will show both the request headers and response headers and show any errors etc:

$ curl -s -v -o /dev/null https://electrictoolbox.com/
*   Trying 173.230.149.142...
* Connected to www.electrictoolbox.com (173.230.149.142) port 80 (#0)
> GET / HTTP/1.1
> Host: www.electrictoolbox.com
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: nginx/1.6.3
< Date: Sun, 23 Aug 2015 21:44:45 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
< 
{ [2688 bytes data]
* Connection #0 to host www.electrictoolbox.com left intact