[Solved] 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?

Enquirer: kdt


Solution #1:

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.

Respondent: pberlijn

Solution #2:

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 will change the call to a HEAD call which will fetch response overhead only, without the body.

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


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

Solution #3:

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.

Respondent: Cyril David

Solution #4:

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
Respondent: Enrico Susatyo

Solution #5:

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.

Respondent: Heath Borders

Solution #6:

Redefine curl output:

curl -sw '%{http_code}'

Can be used with any request type.

Respondent: Grzegorz Luczywo

Solution #7:

Status code ONLY

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

All credit to this GIST

Respondent: mahatmanich

Solution #8:

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 "[email protected]" | sed '$d'
_curl_with_error_code () {
    local curl_error_code http_code
    exec 17>&1
    http_code=$(curl --write-out '
' "[email protected]" | 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[.

Respondent: Lucas Cimon

Solution #9:

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
Respondent: Filip Spiridonov

Solution #10:

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]'
Respondent: zafar142003

Solution #11:

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.

Respondent: Savitoj Singh

Solution #12:

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
Respondent: sorin

Solution #13:

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
Respondent: srana

Solution #14:

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
" ;;
        404) printf "Received: HTTP $response (file not found) ==> $url
" ;;
          *) printf "Received: HTTP $response ==> $url
" ;;
Respondent: Stuart Cardall

Solution #15:

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

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

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

curl --silent -o >(cat >&1) -w "%{http_code}
" 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.

Respondent: Jaakko

Solution #16:

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  "
http: %{http_code} %{size_download}
" -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.

Respondent: sakumatto

Solution #17:

$ curl -kv 2>&1 | grep -i 'HTTP/1.1 ' | awk '{print $3}'| sed -e 's/^[ 	]*//'
  • 2>&1: error is stored in output for parsing
  • grep: filter the response code line from output
  • awk: filters out the response code from response code line
  • sed: removes any leading white spaces
Respondent: Nitish Kumar

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy