Saturday, April 20, 2013

Get HTTP status code using curl from command line

Suppose, I want to test the availability of a website from command line, curl comes to the aid.

The following curl options are really very useful


 -s/--silent
              Silent or quiet mode. Don't show progress meter or error messages.  Makes Curl mute.


-I/--head
              (HTTP/FTP/FILE)  Fetch  the  HTTP-header only! HTTP-servers feature the command HEAD which this uses to get nothing but the header of a document. When used on a FTP or FILE file, curl displays the file size and last modification time only.

 -L/--location
              (HTTP/HTTPS)  If  the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place. If used  together  with  -i/--include  or  -I/--head, headers  from all requested pages will be shown.


Let us illustrate with an example


$ curl -I google.com

HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Sat, 20 Apr 2013 14:14:56 GMT
Expires: Mon, 20 May 2013 14:14:56 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN

So here the HTTP status code while accessing the url, google.com, is 301, indicating a redirect. However, we do not have any info about the url to which google.com redirects. So we use the -L option of curl command as follows.

$ curl -IL google.com


HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Sat, 20 Apr 2013 14:15:00 GMT
Expires: Mon, 20 May 2013 14:15:00 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN

HTTP/1.1 302 Found
Location: http://www.google.co.in/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=34355ea70d42cfd3:FF=0:TM=1366467300:LM=1366467300:S=lf21z4mtM-zoJzkp; expires=Mon, 20-Apr-2015 14:15:00 GMT; path=/; domain=.google.com
Set-Cookie: NID=67=U_DZQR302SSX-7TZo3M6w0aaSBgj6l32BvBjrRO1i4Sk8Ecy6YzKDK5HBewGsgf5bB4sQI_PVRzCeeYfkUlT10X57aqV7jGBFGUx9JcAMZ0rjbIFNggpQULTqCAjil_n; expires=Sun, 20-Oct-2013 14:15:00 GMT; path=/; domain=.google.com; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Date: Sat, 20 Apr 2013 14:15:00 GMT
Server: gws
Content-Length: 221
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN

HTTP/1.1 200 OK
Date: Sat, 20 Apr 2013 14:15:01 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=61af3f5ebcc415a0:FF=0:TM=1366467301:LM=1366467301:S=76cA-ULyN5yq8JmR; expires=Mon, 20-Apr-2015 14:15:01 GMT; path=/; domain=.google.co.in
Set-Cookie: NID=67=uTayihoLkuwaP5vaOmQHcWVs9jJVOdKg1JucLn7Vcybi_t0_b25EZw6qAvPNYRQLkTX0Y56P5YSUWjEyd6EmcZWkPTBhxSs7jrdf0ndfpCpYcrLspEVt9SG2WCRyxzCh; expires=Sun, 20-Oct-2013 14:15:01 GMT; path=/; domain=.google.co.in; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked


To just get the status code alone without any header information

curl -sL -w "%{http_code} %{url_effective}\\n" "URL" -o /dev/null

$ curl -sL -w "%{http_code} %{url_effective}\\n" "http://here.com" -o /dev/null
200 http://here.com

No comments:

Post a Comment