Card 01/ 09
Status Codes: Five Classes, Five Meanings
The first digit says what happened and whose problem it is before the client reads anything else.
An HTTP status code is a three-digit number at the start of every response, and its first digit puts it in one of five classes: 1xx means keep going, 2xx means it worked, 3xx means look elsewhere, 4xx means the request was wrong, and 5xx means the server failed on a request that may have been fine. A client should branch on the class first, because the class alone tells it whose problem the response is and whether trying again could help.
| Class | Says | At fault | Client's next step |
|---|---|---|---|
| 1xx Informational | Keep going; the final answer is still coming | Nobody | Wait for the final response |
| 2xx Success | The request worked | Nobody | Use the response |
| 3xx Redirection | The answer is somewhere else | Nobody | Follow the Location header |
| 4xx Client error | The request was wrong | The client | Fix the request; sending it unchanged will fail again |
| 5xx Server error | The server failed | The server | Retry later if the method is safe to repeat |
Why the class matters more than the code. HTTP defines dozens of codes, and servers may invent new ones. A client that meets a code it does not know must treat it like the first code of its class, x00. So a client that has never heard of 418 still knows it is a client error, and one that meets 599 knows the server failed.
The two classes that matter most. The line between 4xx and 5xx decides retries. A 4xx says the request itself is the problem: a missing field, an unknown resource, a caller with no permission. Repeating it unchanged gets the same answer and only adds load. A 5xx says the server could not do something it should have, so the same request may succeed a moment later.
1xx in APIs. Codes such as 100 Continue and 101 Switching Protocols are sent before the final response, and HTTP libraries handle them without the application seeing them. An API designer rarely sends one on purpose.
The reason phrase is decoration. The words after the number, such as "Not Found", are for people reading a log. Software reads the number, and HTTP/2 does not send the phrase at all.
So choose a code by asking two questions: did it work, and if not, who must change something for it to work?
← → move through sections and cards