Skip to content
Sign inGet all access
Chapter 4·14 minFree

Status Codes

The five classes, and the dozen worth knowing by heart.

Three digits. The first one is the only one you have to memorise.

The five classes

Class Means You should
1xx still thinking keep waiting
2xx it worked carry on
3xx look elsewhere follow, or stop
4xx your request was wrong fix it and do not retry
5xx the server failed retry, maybe

Success with and without a body

200 carries one, and Content-Length says how long:

GET / HTTP/1.1
Host: localhost
Connection: close
OutputHTTP/1.1 200 OKServer: nginx/1.27.5Date: <now>Content-Type: text/htmlContent-Length: 615

304 is a success that deliberately sends none — 175 bytes in total:

GET / HTTP/1.1
Host: localhost
If-None-Match: "67ffa8c6-267"
Connection: close
OutputHTTP/1.1 304 Not Modified

201 reports what was created, in Location:

terminal
{ printf 'HTTP/1.1 201 Created\r\n'
  printf 'Location: /users/1\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/users | head -3
OutputHTTP/1.1 201 CreatedLocation: /users/1Content-Length: 0

Redirects

The code is the instruction; Location is the address:

terminal
{ printf 'HTTP/1.1 301 Moved Permanently\r\n'
  printf 'Location: http://localhost:8080/\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/old | head -2
OutputHTTP/1.1 301 Moved PermanentlyLocation: http://localhost:8080/
Code Permanent Keeps the method
301 yes no
302 no no
303 no no — always GET
307 no yes
308 yes yes

The method after a redirect

A POST through a 302 arrives as a GET:

terminal
{ printf 'HTTP/1.1 302 Found\r\n'
  printf 'Location: http://localhost:8080/\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sv -d 'a=1' -L -o /dev/null localhost:9000/old 2>&1 \
  | grep -E '^[<>] (POST|GET|HTTP|Location)'
Output> POST /old HTTP/1.1< HTTP/1.1 302 Found< Location: http://localhost:8080/> GET / HTTP/1.1< HTTP/1.1 200 OK

Through a 307 it stays a POST — and nginx refuses it, which proves the method survived:

terminal
{ printf 'HTTP/1.1 307 Temporary Redirect\r\n'
  printf 'Location: http://localhost:8080/\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sv -d 'a=1' -L -o /dev/null localhost:9000/old 2>&1 \
  | grep -E '^[<>] (POST|GET|HTTP|Location)'
Output> POST /old HTTP/1.1< HTTP/1.1 307 Temporary Redirect< Location: http://localhost:8080/> POST / HTTP/1.1< HTTP/1.1 405 Not Allowed

Following them

curl does not follow by default — which is what you want when debugging, since a client that follows silently hides the hop going wrong. -L follows and counts:

terminal
{ printf 'HTTP/1.1 301 Moved Permanently\r\n'
  printf 'Location: http://localhost:8080/\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sIL -o /dev/null localhost:9000/old \
  -w 'redirects=%{num_redirects} final=%{http_code}'
Outputredirects=1 final=200

400, 401, 403

400 means the bytes did not parse:

GARBAGE
OutputHTTP/1.1 400 Bad Request

401 and 403 both refuse you, and the difference is whether the server knows who you are. The three read as a sequence:

Code Means What fixes it
400 unparseable correct syntax
401 not authenticated credentials
403 authenticated, not allowed different credentials, or nothing

404 and 410

404 is the one everybody knows:

GET /nope HTTP/1.1
Host: localhost
Connection: close
OutputHTTP/1.1 404 Not Found

410 Gone says it existed and was removed on purpose. A crawler retries a 404 and drops a 410, which is the whole practical difference:

terminal
{ printf 'HTTP/1.1 410 Gone\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/old-page | head -1
OutputHTTP/1.1 410 Gone

The ones past 404

nginx produces three without any help:

GET / HTTP/1.1
Host: localhost
Range: bytes=99999-
Connection: close
OutputHTTP/1.1 416 Requested Range Not Satisfiable
POST / HTTP/1.1
Host: localhost
Content-Length: 99999999
Connection: close
OutputHTTP/1.1 413 Request Entity Too Large
DELETE / HTTP/1.1
Host: localhost
Content-Length: 0
Connection: close
OutputHTTP/1.1 405 Not Allowed

Those three come from the server’s own limits. Three more are application decisions you will meet in any API:

Code Means
409 Conflict the request fights the current state
422 Unprocessable syntax fine, contents wrong
429 Too Many Requests slow down, and read Retry-After

5xx: whose fault

A 5xx is the server admitting fault, and the code says which machine. The two proxy codes are the ones worth telling apart:

Code Who Means
500 the server it crashed handling your request
502 a proxy the thing behind it answered badly
503 the server overloaded or down on purpose
504 a proxy the thing behind it never answered
terminal
{ printf 'HTTP/1.1 503 Service Unavailable\r\n'
  printf 'Retry-After: 120\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | head -2
OutputHTTP/1.1 503 Service UnavailableRetry-After: 120