Shell Script: Get HTTP Header

Objective: Get HTTP response headers of a URL in a shell script.

We can use either curl or wget to get HTTP headers.

For curl, use the following syntax.

$ curl -I www.google.com
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.com.sg/?gfe_rd=cr&ei=wWY7VeCxM6KW8QflkoCADw
Content-Length: 262
Date: Sat, 25 Apr 2015 10:04:49 GMT
Server: GFE/2.0
Alternate-Protocol: 80:quic,p=1

wget normally follows HTTP redirect (HTTP status code 30x). To prevent wget from following redirects, we have to use the “--max-redirect 0” option. Use wget with the following syntax.

$ wget --server-response  --max-redirect 0 --quiet www.google.com
  HTTP/1.1 302 Found
  Cache-Control: private
  Content-Type: text/html; charset=UTF-8
  Location: http://www.google.com.sg/?gfe_rd=cr&ei=emg7Vda2LqqW8QeG-IGICw
  Content-Length: 262
  Date: Sat, 25 Apr 2015 10:12:10 GMT
  Server: GFE/2.0
  Alternate-Protocol: 80:quic,p=1

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, perl, php, python, java, javascript, nodejs, angular, react); plays_on(xbox, ps4); linux_desktop_user(true); }