Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
HTTP in Depth  ›  Lesson

HTTP Status Codes

HTTP Status Codes 14 minThe first digit of a status code says who owns the outcome: 2xx success, 4xx a problem with the request, 5xx a problem on the server.

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

The shortcut

Ask three questions in order: Did it work? If not, whose fault? Then, which exact reason?

Member 7 tries to borrow book 42, which is already outStep 1 of 3
  1. Did it work?

    No loan was created, so not 2xx.

The biggest trap is returning 200 OK with {"error": "book unavailable"} in the body. Every client, monitor and retry library reads the status first, and 200 tells them all it succeeded.

Drills

Answer fast — trust the rule.

quick drill

A request to BookNook returns 503. Whose side is the problem on?

quick drill

A POST creates member 88. Which response fits best?

quick drill

A signed-in member asks to see another member's loan history and is not allowed. Which code?

quick drill

A new book is sent with "pages": -5. The JSON parses fine. Which code fits best?

quick drill

Which failed request is worth retrying later without changing it?

Remember

  • The first digit says who: 2 fine, 4 you, 5 me.
  • Shortcut: did it work, whose fault, which reason.
  • Trap: 200 with an error body, and mixing up 401 (who are you?) with 403 (not allowed).

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainChoose the code

Give the status code for each BookNook situation and justify it: a member with no token asks for their loans; a signed-in member asks for someone else's loans; a member borrows a book that is already on loan; the database is down.