HTTP Status Codes
A borrow request fails. Was the book already on loan? Did the member forget to sign in? Did the server crash? A client program cannot read an apology, but it can read a three-digit number. The status code is how BookNook tells a machine what happened, before it reads a single byte of the body.
The rule
The first digit says who: 2 fine, 4 you, 5 me.
Status codes come in families, and the first digit tells you most of what you need:
- 1xx informational: rarely seen in API work.
- 2xx success: the request did what it asked.
- 3xx redirection: look elsewhere, or use your cached copy.
- 4xx client error: the request was wrong; retrying it unchanged will usually fail again. (429 is the exception: wait, then retry.)
- 5xx server error: the server failed; the same request may work later.
The codes you will reach for most:
| Code | Use it when |
|---|---|
200 OK |
A read or update succeeded and returns a body |
201 Created |
A POST created something; add a Location header |
204 No Content |
It worked and there is nothing to return, as after DELETE |
304 Not Modified |
The client's cached copy is still good |
400 Bad Request |
The request is malformed, such as broken JSON |
401 Unauthorized |
No valid credentials: "who are you?" |
403 Forbidden |
Known user, but not allowed: "I know you, and no" |
404 Not Found |
No such resource |
409 Conflict |
Clashes with current state, such as borrowing a book already on loan |
422 Unprocessable Content |
Well-formed, but the values break a rule |
429 Too Many Requests |
The client hit its rate limit |
500 Internal Server Error |
An unexpected bug on the server |
503 Service Unavailable |
Temporarily overloaded or down for maintenance |