liveBook · Manning

HTTP 404 error

As explained in The Design of Web APIs, Second Edition

404 is the HTTP status code indicating that the requested resource could not be found.

HTTP 404 Not Found indicates that the requested resource could not be found. It assigns responsibility to the client-side request when a resource identified by a path parameter does not exist. section 4.5.10 section 4.5.8

Use 404 when:

  • A request targets a nonexistent resource, such as GET /products/{productReference} for an unknown product. section 4.5.8
  • A DELETE operation targets a resource that does not exist; the response should use 404 rather than falsely reporting success. section 3.1.3
  • The API must avoid disclosing whether a protected resource exists, such as an account outside the caller’s permitted view. section 12.10.2

Missing product

A consumer sends DELETE /products/123 when product 123 does not exist. A compliant response is 404 Not Found. By contrast, 204 No Content means the product exists and the operation intentionally returns no data; 400 Bad Request is appropriate for missing or invalid input. section 4.5.10 section 3.1.3

Listing 6.11 Response without a body
...
paths:
  /products/{productReference}:
    summary: Product
    ...
    delete:
      summary: Remove a product from the catalog
      responses:
        "204":
          description: Product removed
 #1
        "404":
          description: No product found
          content:                        #2
            application/json:
              schema:
                description: Error info.

An OpenAPI response definition gives the 404 response an error description and JSON content, unlike a 204 response without a body. section 6.8.2

Figure 12.12 The “Read an account” operation returns “403” if the consumer doesn’t have the proper scope or if the end user is a bank advisor who doesn’t manage the account. It also returns “404” if the end user is an account owner who does not own the account.
figure

A permission example shows when 404 conceals a protected account and when 403 Forbidden can safely communicate an access problem. section 12.10.2

How The Design of Web APIs, Second Edition covers it

How deep it goes
Explains resource absence, distinguishes 404 from 204 and 400, and extends the choice to permission-related disclosure.
Examples
Uses nonexistent products, DELETE requests, OpenAPI response definitions, and inaccessible bank accounts.
What it assumes
Assumes familiarity with HTTP requests and status classes while distinguishing client errors from server errors.
What you can do after
Apply 404 in HTTP and OpenAPI responses when resources are absent or their existence must remain undisclosed.
How it connects
Connects HTTP status codes with REST, OpenAPI, resource paths, error responses, authentication, and authorization.

Also explained in

Compare how 3 books explain HTTP 404 error →

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.