Card 08/ 09

ExerciseDifficulty: Intermediate1 min

Practice: The Right Code for Eight Responses

A created booking, a duplicate username, an expired token, a missing permission, a timeout upstream and three more.

Choosing the status code from what happened, and knowing what the client will do with it, is the skill every endpoint needs. For each scenario on a hotel booking API, choose the status code and say who must act next.

  1. POST /bookings creates booking 81.
  2. POST /users with a username that is already taken.
  3. A request with a token that expired an hour ago.
  4. A signed-in guest calls DELETE /hotels/5, which only staff may do.
  5. The gateway waited 30 seconds for the booking service and gave up.
  6. POST /bookings with a check-out date before the check-in date.
  7. DELETE /bookings/81/notes/3 succeeds, with nothing to return.
  8. The whole API is down for planned maintenance for the next ten minutes.
Show the answers
Each scenario, its code, and who acts next
ScenarioCodeWho acts next
Booking created201 Created, with Location /bookings/81Client stores the URL
Username taken409 ConflictClient chooses another name
Expired token401 UnauthorizedClient refreshes the token and retries
Guest deletes a hotel403 ForbiddenNobody; the guest may not do this
Upstream timeout504 Gateway TimeoutServer team; the client checks before retrying the POST
Check-out before check-in422 Unprocessable Content, or 400Client corrects the dates
Note deleted204 No ContentNobody
Planned maintenance503 Service Unavailable, with Retry-After 600Client waits and retries