Sign up ×
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
As for me, I can see from the manual how to get the HTTP status code, but the option -w does not work. I have reported the bug to Apple. –  Nicolas Barbulesco May 4 at 17:51
It's time to switch the accepted answer, me thinks! :P –  pvandenberk Sep 18 at 12:48

7 Answers 7

up vote 95 down vote accepted

This should work for you:

curl -I

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 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
Following the redirect after only doing a HEAD request may cause interesting behavior, depending on how the app is programmed. –  smackfu Sep 21 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}"

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}"
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
curl --verbose

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

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
the best answer as it makes curl output both headers and body, making it suitable for most of the tasks when used in a script –  Sarge Borsch Aug 31 at 19:52

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) '')
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
The output is in STDOUT, so you should be able to redirect output from the command to anywhere you like just like a regular command. I haven't tested this though. –  Heath Borders Jul 21 at 3:10

Redefine curl output:

curl -sw '%{http_code}'

Can be used with any request type.

share|improve this answer

Only the status code:

curl -I 2>/dev/null | head -n 1 | cut -d$' ' -f2
share|improve this answer
Can you explain what this code does and how it addresses the problem given by the OP? Unexplained code can appear untrusted and dangerous to users. –  DragonLord Jul 16 at 1:58
Sure, we send a request to url, get only the first line of the response, split it on blocks and select the second one. It contains the response code that OP is looking for. –  Filip Spiridonov Jul 20 at 22:01

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.