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: close304 is a success that deliberately sends none — 175 bytes in total:
GET / HTTP/1.1
Host: localhost
If-None-Match: "67ffa8c6-267"
Connection: close201 reports what was created, in Location:
{ 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 -3Redirects
The code is the instruction; Location is the address:
{ 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| 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:
{ 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)'Through a 307 it stays a POST — and nginx refuses it, which proves the method
survived:
{ 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)'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:
{ 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}'400, 401, 403
400 means the bytes did not parse:
GARBAGE401 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: close410 Gone says it existed and was removed on purpose. A crawler retries a 404 and drops
a 410, which is the whole practical difference:
{ 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 -1The ones past 404
nginx produces three without any help:
GET / HTTP/1.1
Host: localhost
Range: bytes=99999-
Connection: closePOST / HTTP/1.1
Host: localhost
Content-Length: 99999999
Connection: closeDELETE / HTTP/1.1
Host: localhost
Content-Length: 0
Connection: closeThose 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 |
{ 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