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.
1 2 3 4 5 6 7 8 9 |
$ 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.
1 2 3 4 5 6 7 8 9 |
$ 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 |