Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm using curl at the command line on Linux to issue HTTP requests. The response bodies are printed to standard out, which is fine, but I can't see from the man page how to get curl to print the HTTP status code from the response (404, 403 etc). Is this possible?

share|improve this question

5 Answers 5

up vote 51 down vote accepted

This should work for you:

curl -I http://www.example.org

As an addition, to let cURL follow redirects (3xx statuses) add -L.

share|improve this answer
23  
NB: curl -I does a HEAD HTTP request, which can be problematic for testing the HTTP status code for some web application servers and services –  Jay Taylor Sep 6 '12 at 17:32
2  
And to get just the status number, pipe it to head -n 1|cut -d$' ' -f2 –  Benubird Jul 17 '13 at 11:33
2  
Don't forget to redirect curl's stderr: curl -I http://www.example.org 2>/dev/null | head -n 1 | cut -d$' ' -f2. Add -L to curl if you need the final status after redirects. –  Aaron Blenkush Jul 24 at 21:16

A more specific way to print out just the HTTP status code is something along the lines of:

curl -s -o /dev/null -w "%{http_code}" http://www.example.org/

A lot easier to work with in scripts, as it doesn't require any parsing :-)

The parameter -I might be added to improve response load performance. This parameter just request for status/headers of response, without download response body. (%{http_code} returns on first line of HTTP payload)

i.e.:

curl -s -o /dev/null -I -w "%{http_code}" http://www.example.org/
share|improve this answer
3  
-w "%{http_code}" is the bit that prints the status code. You can add a newline or two in there to separate the code from the body (-w "\n\n%{http_code}\n") –  Jeffrey Martinez Jan 11 at 8:33

If you want to see the header as well as the result you can also use -v:

curl -v http://www.example.org
share|improve this answer
4  
+1 for pointing out the verbose flag provides the extra details. Great for testing REST apps. –  MrOodles Oct 15 '12 at 20:37
1  
+1 very easy to use when doing POST request (curl -v --data "...") –  MegaTux Jun 23 at 20:06

You can print the status code, in addition to all the headers by doing the following:

curl -i http://example.org

The good thing about -i is that it works with -X POST as well.

share|improve this answer
    
Much better than the accepted answer (which does a HEAD request). –  neu242 Oct 2 at 10:05
    
Maybe obvious, but -i does work with any HTTP method, not just GET and POST... :) –  mac Oct 20 at 10:35

Redefine curl output:

curl -sw '%{http_code}' http://example.org

Can be used with any request type.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.