How to view HTTP headers from the command line using curl

I’ve been working a lot on the next generation web server for the alvinalexander.com website lately, and as I’m looking at different HTTP accelerators, I keep wanting/needing to look at the headers returned by my web pages. A simple way to look at the HTTP headers from the command line is with the curl command, like this:

curl -I http://example.com/

Running this command against the main Google website, I see output like this:

HTTP/1.1 200 OK
Date: Tue, 06 Dec 2011 00:57:08 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=long-string-here; expires=Thu, 05-Dec-2013 00:57:08 GMT; path=/; domain=.google.com
Set-Cookie: NID=very-long-string-here; expires=Wed, 06-Jun-2012 00:57:08 GMT; path=/; domain=.google.com; HttpOnly
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked

Personally I can’t remember the -I flag, so I use the --head flag, which is easier for me to remember:

curl --head http://example.com

You can do something similar with the telnet command, connecting to a website on port 80, and then using the HTTP HEAD command, but I’ve found that second curl command much easier to remember.

In summary, if you ever need to look at the HTTP headers from a website or web application, I hope this tip on using the Unix/Linux curl command has been helpful.