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

6 Answers 6

up vote 65 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
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
And to get just the status number, pipe it to head -n 1|cut -d$' ' -f2 –  Benubird Jul 17 '13 at 11:33
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 '14 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)


curl -s -o /dev/null -I -w "%{http_code}" http://www.example.org/
share|improve this answer
-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 '14 at 8:33
Wow, this /dev/null thing even works in the Windows version of curl that I'm using. –  Uwe Keim Jan 30 at 6:53

If you want to see the header as well as the result you can use the verbose option:

curl -v http://www.example.org
curl --verbose http://www.example.org

The status will appear in the header. E.g.

< Date: Tue, 04 Nov 2014 19:12:59 GMT
< Content-Type: application/json; charset=utf-8
< Status: 422 Unprocessable Entity
share|improve this answer
+1 for pointing out the verbose flag provides the extra details. Great for testing REST apps. –  MrOodles Oct 15 '12 at 20:37
+1 very easy to use when doing POST request (curl -v --data "...") –  MegaTux Jun 23 '14 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 '14 at 10:05
Maybe obvious, but -i does work with any HTTP method, not just GET and POST... :) –  mac Oct 20 '14 at 10:35

If you want to capture the HTTP status code in a variable, but still redirect the content to STDOUT, you must create two STDOUTs. You can do so with process substitution >() and command substitution $().

First, create a file descriptor 3 for your current process' STDOUT with exec 3>&1.

Then, use curl's -o option to redirect the response content to a temporary fifo using command substitution, and then within that command substitution, redirect output back to your current process STDOUT file descriptor 3 with -o >(cat >&3).

Putting it all together:

#creates a new file descriptor 3 that redirects to 1 (STDOUT)
exec 3>&1 
# Run curl in a separate command, capturing output of -w "%{http_code}" into HTTP_STATUS
# and sending the content to this command's STDOUT with -o >(cat >&3)
HTTP_STATUS=$(curl -w "%{http_code}" -o >(cat >&3) 'http://example.com')
share|improve this answer
That's serious slickery...and I like it! –  spyle Jan 30 at 21:14
Now how, in turn, can I redirect the output to another variable? –  Roger Filmyer Mar 12 at 1:46

Redefine curl output:

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

Can be used with any request type.

share|improve this answer

Your Answer


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.