Getting curl to output HTTP status code?



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?


Posted 2011-04-18T10:28:57.347

Reputation: 9 203

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 – 2015-05-04T17:51:37.817


The -i flag, as in curl -i, is probably what you want, as per

– caw – 2017-03-13T03:10:06.357

Why not just something like curl -IL | grep "^HTTP\/" ? – St3an – 2019-02-18T07:46:14.017

Not to future self: the answer you want is probably Cyril David's (currently in 4th position) – WhiteHotLoveTiger – 2019-06-17T18:08:54.990



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

curl -I

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


Posted 2011-04-18T10:28:57.347

Reputation: 6 138

36Don'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 – 2014-07-24T21:16:46.683

1Following the redirect after only doing a HEAD request may cause interesting behavior, depending on how the app is programmed. – Scott McIntyre – 2015-09-21T21:16:08.223

36curl -I -X GET will send a GET request, but give the same output. – jiggy – 2015-11-30T19:20:17.503

Here are two working examples, for GET and HEAD --

– sorin – 2016-06-23T10:38:19.993

1Forwarding stderr to /dev/null is not neccessary if you only want to suppress the progress bar - use -s switch for that. – galva – 2016-12-12T09:39:43.620

Not useful for testing services that return status codes for other methods than GET. Like, REST services. – Keith Tyler – 2017-03-24T22:33:31.603

This command shows only some info. I can't see my response. "-i" may be better. – emeraldhieu – 2017-12-13T04:12:31.933

161NB: 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 – 2012-09-06T17:32:41.517

17And to get just the status number, pipe it to head -n 1|cut -d$' ' -f2 – Benubird – 2013-07-17T11:33:40.010


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.

Note: %{http_code} returns on first line of HTTP payload


curl -s -o /dev/null -I -w "%{http_code}"


Posted 2011-04-18T10:28:57.347

Reputation: 9 171

10Wow, this /dev/null thing even works in the Windows version of curl that I'm using. – Uwe Keim – 2015-01-30T06:53:44.373

3I 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. – RomanSt – 2015-12-13T23:59:29.037

@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 – 2016-06-17T15:56:54.533

45FYI: -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 – 2016-07-19T16:59:51.387

...and -I = Show document info only – Madbreaks – 2016-10-13T04:37:08.330

1Are the quotes around the "%{http_code}" required ? – Hakan Baba – 2018-03-13T18:52:52.887

I had to add '-LI' param just before the URL to be able to correctly get the last status after a redirects rather than the '302' (based on @mahatmanich answer): curl -s -o /dev/null -w "%{http_code}" -LI

– Maksym – 2018-03-16T14:17:19.247

As Maksym noticed you will need to add -L in order to follow redirects, otherwise your status code will simply be 302 every time you encounter a page that has moved with a 301 or 302. – dragon788 – 2018-10-05T16:38:14.600

@HakanBaba - The quotes are optional, but if you want to include newline or other escaped characters you'll need quotes. – Surreal Dreams – 2019-05-01T16:00:47.317

for windows, use -o Nul rather than /dev/null – p_champ – 2019-06-26T01:37:51.657

@Ajedi32 I’m pretty sure no command can write anything to stdout after exit… ;-) – Raphael Schweikert – 2019-09-10T13:56:07.643

The flag -w is shorthand for --write-out <format> which is described as "display information on stdout after a completed transfer" I wish there was a --write-err to do the same thing, but on stderr so you could collect it separately. Any suggestions? – Bruno Bronosky – 2019-10-08T00:31:14.683

63-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 – 2014-01-11T08:33:18.607


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

Enrico Susatyo

Posted 2011-04-18T10:28:57.347

Reputation: 2 928

8+1 very easy to use when doing POST request (curl -v --data "...") – MegaTux – 2014-06-23T20:06:23.480

1It even splits them in two different file outputs (http status details to stderr and response body to stdout) – phil294 – 2018-04-30T07:37:21.233

29+1 for pointing out the verbose flag provides the extra details. Great for testing REST apps. – MrOodles – 2012-10-15T20:37:25.027


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.

Cyril David

Posted 2011-04-18T10:28:57.347

Reputation: 2 181

40Much better than the accepted answer (which does a HEAD request). – neu242 – 2014-10-02T10:05:55.973

10Maybe obvious, but -i does work with any HTTP method, not just GET and POST... :) – mac – 2014-10-20T10:35:34.390

4the best answer as it makes curl output both headers and body, making it suitable for most of the tasks when used in a script – Display Name – 2015-08-31T19:52:06.623

6This is the best answer, and can be used in conjunction with -s (don't show progress meter or error messages) and -S(do show error messages after all) – Jonathan Hartley – 2017-02-23T19:10:56.137


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 in bash 3.2.57(1)-release (standard for macOS):

# 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) '')

Note that this doesn't work in /bin/sh as SamK noted in the comments below.

Heath Borders

Posted 2011-04-18T10:28:57.347

Reputation: 838

5That's serious slickery...and I like it! – spyle – 2015-01-30T21:14:20.173

3Now how, in turn, can I redirect the output to another variable? – Roger Filmyer – 2015-03-12T01:46:24.417

1The 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 – 2015-07-21T03:10:27.767

1Does not work with /bin/sh. – SamK – 2018-10-11T14:47:36.937

good answer, you can also redirect to a real file and cat it later if you want portability of shells – akostadinov – 2019-02-08T09:40:19.273

Is it necessary to close the file descriptor (exec 3>&-) after curl has finished? – martsraits – 2019-05-31T07:01:38.147

Good, bug fails in cygwin, Warning: Failed to create the file /dev/fd/63: No such file or directory. Use -o curl_res.txt – Tuntable – 2019-12-01T06:15:24.597


Redefine curl output:

curl -sw '%{http_code}'

Can be used with any request type.

Grzegorz Luczywo

Posted 2011-04-18T10:28:57.347

Reputation: 459

-k (--insecure) is overriding -s (silent). – Ravichandra – 2018-07-10T11:33:34.267


Status code ONLY

[0]$ curl -LI -o /dev/null -w '%{http_code}\n' -s
[0]$ 200

All credit to this GIST


Posted 2011-04-18T10:28:57.347

Reputation: 315


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)
    exec 17>&-
    if [ $curl_error_code -ne 0 ]; then
        return $curl_error_code
    if [ $http_code -ge 400 ] && [ $http_code -lt 600 ]; then
        echo "HTTP $http_code" >&2
        return 127

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[.

Lucas Cimon

Posted 2011-04-18T10:28:57.347

Reputation: 373


Agreed, not being able to see the error output is a painful limitation of the otherwise very handy --fail. How can you diagnose a REST api failure without seeing the error output? It's so unfortunate that the curl maintainer bagder stubbornly insists on not providing a --fail-but-show-error.

– jamshid – 2018-05-13T23:33:53.913

As stated in documentation, it doesn't work for 401 and 407 HTTP code :( – Logan Mzz – 2019-08-29T08:47:41.380


This will 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

curl -I 2>/dev/null | head -n 1 | cut -d$' ' -f2

Filip Spiridonov

Posted 2011-04-18T10:28:57.347

Reputation: 211

1Can 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 – 2015-07-16T01:58:18.590

1Sure, 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 – 2015-07-20T22:01:20.790


For a POST request, the following worked:

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


Posted 2011-04-18T10:28:57.347

Reputation: 191


Use the following cURL command and pipe it to grep like so:

$ curl -I -s -L | grep "HTTP/1.1"

Here's what each flag does:

  • -I: Show only response headers
  • -s: Silent - Don't show progress bar
  • -L: Follow Location: headers

Here is a link to HTTP status codes.

Run from the command line. This curl runs in silent mode, follows any redirects, get the HTTP headers. grep will print the HTTP status code to standard output.

Savitoj Singh

Posted 2011-04-18T10:28:57.347

Reputation: 91


curl -so -i /dev/null -w "%{http_code}"

This will return the following information:

  1. response data, if any data is returned by API like error
  2. status code


Posted 2011-04-18T10:28:57.347

Reputation: 51

This doesn't follow redirects. This existing answer is better

– cricket_007 – 2017-04-10T18:16:34.643

Sure, you can refer that as well!! – srana – 2017-12-12T16:40:08.083


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

curl -so /dev/null -w '%{response_code}'

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


Posted 2011-04-18T10:28:57.347

Reputation: 9 439

Not working on OS X at least. – Ain – 2016-07-27T14:01:56.703

Working fine for me on OS X High Sierra 10.13.6. – Ben Baron – 2018-07-27T16:39:33.627


An example of how to use the response codes. I use this to re-download Geolite databases only if they have changed (-z) & also following redirects (-L):

file=$(basename $url)

response=$(curl -L -s -o $file -z $file $url -w "%{http_code}")

case "$response" in
        200) do_something ;;
        301) do_something ;;
        304) printf "Received: HTTP $response (file unchanged) ==> $url\n" ;;
        404) printf "Received: HTTP $response (file not found) ==> $url\n" ;;
          *) printf "Received: HTTP $response ==> $url\n" ;;

Stuart Cardall

Posted 2011-04-18T10:28:57.347

Reputation: 236


The OP wants to know the status code. Often when downloading a file you also want to get a feel of it's size so I'm using curl first to show status code and size of file and then shut off verbose and direct file to the place and name I want:

curl -R -s -S -w  "\nhttp: %{http_code} %{size_download}\n" -o /Users/myfiles/the_local_name.html

Then I wait for the finishing of curl

wait ${!}

before I run the next command. The above when used in a script of many commands like above gives a nice response like:

http: 200 42824

http: 200 34728

http: 200 35452

Please note that -o in curl needs to be followed by the full path of the file + name of file. This allows you thusly to save files in a sensible name structure when you d/l them with curl. Also note that -s and -S used together silence the output but does show errors. Note also that -R tries to set the file timestamp to that of the web file.

My answer is based on what @pvandenberk originally suggested, but in addition it actually saves the file somewhere, instead of merely directing to /dev/null.


Posted 2011-04-18T10:28:57.347

Reputation: 31


Split output content to stdout and HTTP status code to stderr:

curl -o >(cat >&1) -w "%{http_code}\n" 1>&2

If only HTTP status code is desired to stderr, --silent can be used:

curl --silent -o >(cat >&1) -w "%{http_code}\n" 1>&2

The desired stream can then be picked by redirecting unwanted one to /dev/null:

$ (curl --silent -o >(cat >&1) -w "%{http_code}" 1>&2) 1>/dev/null
$ (curl --silent -o >(cat >&1) -w "%{http_code}" 1>&2) 2>/dev/null
<!doctype html>

Note that for the second redirection to behave as desired, we need to run the curl command in subshell.


Posted 2011-04-18T10:28:57.347

Reputation: 240

1Requires bash for process substitution. – Jaakko – 2019-06-04T08:16:09.870

@Bruno, I changed the example from, as I think the /tmp/out /tmp/err files can cause unexpected results if run parallel.

– Jaakko – 2019-10-08T08:13:26.050