Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 '15 at 17:51
    
It's time to switch the accepted answer, me thinks! :P – pvandenberk Sep 18 '15 at 12:48
up vote 115 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
59  
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
5  
And to get just the status number, pipe it to head -n 1|cut -d$' ' -f2 – Benubird Jul 17 '13 at 11:33
5  
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
    
Following the redirect after only doing a HEAD request may cause interesting behavior, depending on how the app is programmed. – smackfu Sep 21 '15 at 21:16
1  
curl -I -X GET will send a GET request, but give the same output. – jiggy Nov 30 '15 at 19:20

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
10  
-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
3  
Wow, this /dev/null thing even works in the Windows version of curl that I'm using. – Uwe Keim Jan 30 '15 at 6:53
    
I believe this downloads the entire file even though it all goes to /dev/null, so not ideal for checking the status code for huge files. httping -c 1 -s -G -m issues a GET and doesn't download the whole file, although I realise this question is specifically about curl. – romkyns Dec 13 '15 at 23:59

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
7  
+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 '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
5  
Much better than the accepted answer (which does a HEAD request). – neu242 Oct 2 '14 at 10:05
2  
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 '15 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) 'http://example.com')
share|improve this answer
1  
That's serious slickery...and I like it! – spyle Jan 30 '15 at 21:14
3  
Now how, in turn, can I redirect the output to another variable? – Roger Filmyer Mar 12 '15 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 '15 at 3:10

Redefine curl output:

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

Can be used with any request type.

share|improve this answer

For a POST request, the following worked:

curl -w 'RESP_CODE:%{response_code}' -s -X POST --data '{"asda":"asd"}' http://example.com --header "Content-Type:application/json"|grep -o  'RESP_CODE:[1-4][0-9][0-9]'
share|improve this answer

Only the status code:

curl -I http://example.org 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. – bwDraco Jul 16 '15 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 '15 at 22:01

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.