Lesson 2 · Unit 01
Status and errors
The right code for each outcome, and an error body a client can act on
What you practise
- The right code for the situation (200, 201, 204, 400, 401, 403, 404, 409, 422, 500, 503), an error body a client can act on
Try a question from this lesson
Pick one
A client orders an item that is out of stock and gets this reply. The dashboard counts 2xx replies as successes. What goes wrong?
HTTP/1.1 200 OK
Content-Type: application/json
{"error": "out of stock"}Correct answer
Every tool that reads only the status sees a success
Why
Monitoring, client libraries, retry logic and caches all act on the status code and never open the body. A failure dressed as 200 is invisible to them. The status code is the part of the reply that machines read.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Three ways to say yes
200 OK: it worked, and the body holds the result.
201 Created: it worked and made something new. The Location header says where the new thing lives.
204 No Content: it worked and there is nothing to send back, so there is no body to parse.
POST /orders -> 201 Created
Location: /orders/41
GET /orders/41 -> 200 OK {...}
DELETE /orders/41 -> 204 No ContentConcept
Whose fault, and who retries
4xx means the request is the problem. Sending it again unchanged will fail again, so the client must change something first.
5xx means the server is the problem. 500 is a bug or a crash. 503 Service Unavailable means "not now": overloaded or mid-deploy. It may carry Retry-After to say when to come back.
Concept
An error a client can act on
A good error body answers three questions: what kind of problem (for code), what happened (for a person), and which data helps fix it.
The "problem details" format is a standard shape for this. It is sent with the content type application/problem+json, and its status member repeats the HTTP status code.
HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json
{"type": "/errors/validation",
"title": "Invalid order",
"status": 422,
"errors": [{"field": "quantity",
"detail": "must be > 0"}]}8 more exercises in the app
- 1× fill the blanks
- 4× multiple choice
- 1× match the pairs
- 1× true or false
- 1× type the answer
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.