Alex Scotton

App & Web Developer

Check HTTP Headers in Linux with cURL

Posted by Alex Scotton on 29/6/2013

I needed to check some RewriteRules in a new .htaccess file and found a good alternative to just adding another Chrome Extension.

cURL is "a command line tool for getting or sending files using URL syntax", which up until recently I had only used for checking APIs and grabbing my current WAN IP:

$ curl icanhazip.com

However, it is also capable of outputing a range of information about the HTTP transaction including the response headers. Run the following command to see it in action:

$ curl -s -D - example.com -o /dev/null
HTTP/1.0 302 Found
Location: http://example.iana.org
Server: BigIP
Connection: Keep-Alive
Content-Length: 0

You can also add a useragent to your request to check for mobile redirects:

$ curl -s -D - example.com -A "My own custom useragent string" -o /dev/null

Awesome, huh?

It's a little long for my liking though. So, I wrote a short function to make the command much easier to run. I'm a huge fan of zsh, so I pop the following in the .zshrc file in my home directory:

curlHead() {
    if [ ! $1 ] ; then
        print "Usage: \n$ curlHead url useragent"
        return 1
    elif [ $2 ] ; then
        curl -s -D - $1 -A $2 -o /dev/null
    else
        curl -s -D - $1 -o /dev/null
    fi
}

Now I can run:

$ curlHead example.com "My own custom useragent string"

If you have any expansions on the above, please share in the comments below.

Happy HTTP Debugging.