Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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

10 Answers 10

up vote 149 down vote accepted

This should work for you if the web server is able to respond to HEAD requests (this will not perform a GET):

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

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

share|improve this answer
77  
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
8  
And to get just the status number, pipe it to head -n 1|cut -d$' ' -f2 – Benubird Jul 17 '13 at 11:33
9  
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
6  
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
16  
-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
    
@romkyns, you're correct: the first variant in my answer downloads the entire file and "saves" it to /dev/null, but the second variant - ie. the one using the -I option for curl - doesn't. However, care must be taken, as you're not really testing the same action: one does an HTTP GET request, whilst the other makes a HEAD request... some webservers/websites will respond with a different status code, even if the requested URL is exactly the same! – pvandenberk Jun 17 at 15:56
    
FYI: -s = Don't show download progress, -o /dev/null = don't display the body, -w "%{http_code}" = Write http response code to stdout after exit. – Ajedi32 Jul 19 at 16: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
10  
+1 for pointing out the verbose flag provides the extra details. Great for testing REST apps. – MrOodles Oct 15 '12 at 20:37
2  
+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
9  
Much better than the accepted answer (which does a HEAD request). – neu242 Oct 2 '14 at 10:05
3  
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
2  
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

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

This is a painful curl --fail limitation. From man curl :

-f, --fail (HTTP) Fail silently (no output at all) on server errors

But there is no way to get both the non-zero return code AND the response body in stdout.

Based on pvandenberk's answer and this other very useful trick learned on SO, here is a workaround :

curl_with_error_code () {
    _curl_with_error_code "$@" | sed '$d'
}
_curl_with_error_code () {
    local curl_error_code http_code
    exec 17>&1
    http_code=$(curl --write-out '\n%{http_code}\n' "$@" | tee /dev/fd/17 | tail -n 1)
    curl_error_code=$?
    exec 17>&-
    if [ $curl_error_code -ne 0 ]; then
        return $curl_error_code
    fi
    if [ $http_code -ge 400 ] && [ $http_code -lt 600 ]; then
        echo "HTTP $http_code" >&2
        return 127
    fi
}

This function behaves exactly as curl, but will return 127 (a return code non-used by curl) in case of a HTTP code in the range [400, 600[.

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

Here is some curl command that is using GET and that returns the HTTP code.

curl -so /dev/null -w '%{response_code}' http://www.example.org

Please remember that the approach below is using HEAD, which is faster but it may not work well with some web less compliant HTTP servers.

 curl -I http://www.example.org
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .