liveBook · Manning

4xx response

As explained in The Design of Web APIs, Second Edition

A 4xx status class is the class of HTTP status codes indicating that the client request contains an error or cannot be fulfilled as submitted.

A 4xx response uses an HTTP status code from 400 to 499 to indicate that a consumer error prevents the server from processing a request. The status code and its reason phrase identify the outcome; the class also helps interpret unfamiliar codes. section 4.5.1

Choose a 4xx status by considering the error and the request:

  • Use 404 Not Found when a requested resource does not exist; do not substitute 204 No Content, which would mean the resource exists but has no data. section 4.5.8
  • Use 400 Bad Request for incorrect or incomplete input, such as invalid product information. section 4.5.8
  • Choose statuses operation by operation, considering the output, who caused the error, and the request method. Avoid unassigned codes and unusual codes that may surprise consumers. section 4.5.2

Online shopping API

A request for a nonexistent product reference can return 404; a request to add a product with incorrect or incomplete information can return 400. section 4.5.8

How The Design of Web APIs, Second Edition covers it

How deep it goes
Explains class-level meaning and applies 400 and 404 to common errors, while deferring further code choices.
Examples
Uses nonexistent products and incorrect or incomplete product information to illustrate 404 and 400 responses.
What you can do after
Evaluate whether an API error is consumer-caused and select a suitable status for missing resources or invalid input.
How it connects
Connects HTTP status codes with HTTP methods, 2XX success responses, and 5XX server errors.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Design of Web APIs, Second Edition. The section links point to the parts of the book it draws on.