Status codes that tell the truth
Your API returns 200 OK with {"error": "not found"} and everything looks fine, until a cache keeps the error, a retry library gives up too early and the dashboard shows no failures. The status code is what programs read. Here is what each class tells them.
01 Programs read the number, not the body
Every HTTP response starts with a status line: the version, a three-digit code and a short reason phrase. Browsers, fetch, caches, proxies, load balancers, retry libraries, crawlers and monitoring all decide what to do from that number before they look at the body, and many never look at it at all.
The body is for people and for your own client code. The code is the part every program in between understands, so it has to be true.
02 Five classes, by the first digit
The first digit sorts every code into a class with one meaning. A client that does not know a particular code, say 432, must treat it as the x00 of its class, so 432 behaves like 400. The rule is in the HTTP spec (RFC 9110).
| Class | Means | Whose problem | What a client does |
|---|---|---|---|
| 1xx | Hold on, more is coming | Nobody | Waits for the real response (you rarely see these) |
| 2xx | It worked | Nobody | Uses the body; a cache may store it |
| 3xx | Look somewhere else | Nobody | Follows `Location`, or uses its cached copy for `304` |
| 4xx | The request is wrong | The client | Fixes the request; sending it unchanged gets the same answer |
| 5xx | The server failed | The server | May retry later, if the request is safe to repeat |
03 Watch a client act on each class
Here is one request that meets three classes on the way to its answer. Follow the right-hand list: each decision comes from the first digit.
- just sent or decided
- the result
- earlier
An app wants order 42. It holds an old URL for it. Nothing has been sent yet.
04 The codes you will actually send
You do not need all of the roughly 60 registered codes. These cover most APIs.
| Code | Send it when | The client then |
|---|---|---|
| 200 OK | A read or update worked and there is a body | Uses the body |
| 201 Created | A POST made something; add `Location` | Can fetch the new resource |
| 204 No Content | It worked and there is nothing to return | Expects no body |
| 301 / 308 | The URL moved for good | Follows it and may cache the move |
| 302 / 307 | Go elsewhere, just this time | Follows it, keeps the old URL |
| 304 Not Modified | The client’s cached copy is still current | Uses its cache |
| 400 Bad Request | The request is malformed or invalid | Fixes the input |
| 401 Unauthorized | No valid credentials came with it | Logs in, then retries |
| 403 Forbidden | We know who you are; you may not do this | Stops; logging in again won’t help |
| 404 Not Found | Nothing at this URL | Stops, or creates it |
| 409 Conflict | It clashes with the current state | Re-reads, then retries |
| 422 Unprocessable Content | Well-formed, but fails validation | Shows the field errors |
| 429 Too Many Requests | The client is over its rate limit | Waits for `Retry-After`, then retries |
| 500 Internal Server Error | An unexpected bug | May retry, then reports it |
| 502 / 504 | A proxy got a bad or no reply from upstream | May retry later |
| 503 Service Unavailable | Overloaded or down for now | Retries after `Retry-After` |
429 is the one 4xx that asks for a retry. The request is fine; it just came too often. Send Retry-After with it so clients know how long to wait.
05 Codes that sound alike but aren’t
Two pairs cause most confusion. 401 vs 403: 401 means “I don’t know who you are”, so the client should authenticate; the spec requires a WWW-Authenticate header with it. 403 means “I know who you are and the answer is no”, so authenticating again is pointless.
302 vs 307 (and 301 vs 308): for historical reasons, browsers turn a POST into a GET when they follow a 301 or 302. A 307 or 308 must be followed with the same method and body. 303 See Other asks for the switch to GET on purpose, which is the usual answer after a form POST.
POST /api/orders → 302 FoundLocation: /api/v2/ordersGET /api/v2/orders (body dropped)
POST /api/orders → 308 Permanent RedirectLocation: /api/v2/ordersPOST /api/v2/orders (same body)
06 The 200 that lies
Wrapping errors in a 200 breaks everything that reads the number. A CDN may cache the error for every user. A retry policy sees success and stops. Error-rate monitoring counts it as a good request. And fetch sets res.ok to true.
HTTP/1.1 200 OKContent-Type: application/json{"ok": false, "error": "not found"}
HTTP/1.1 404 Not FoundContent-Type: application/json{"error": "not found"}
The truth has to flow the other way too. fetch resolves for every status, including 404 and 500; it only rejects when there is no response at all, such as a network failure. Check res.ok (true for 200 to 299) or res.status yourself.
07 When the code misleads
500 for a bad email fieldA 5xx for the client’s mistakeClients retry it pointlessly and your on-call gets paged. Validate input and answer 400 or 422.400 when the database is downA 4xx for your own failureClients give up on a request that would have worked a minute later. If the server failed, say 5xx.301 to the wrong placeA permanent redirect you regretBrowsers can cache a 301 for a long time, so fixing the server does not fix them. Test with 302 or 307 first.401 for “not your order”401 when you mean 403The client logs in again, gets 401 again and loops. Use 403, or 404 if the order’s existence is itself private.200 with an error bodySuccess that isn’tCaches store it, retries stop and monitoring stays green. Use a 4xx or 5xx and put the detail in the body.