HTTP Bad Request (400) vs Not Found (404): Which status code to use?

A thumbnail showing REST API Status codes.

REST API developers might be tempted to use a 400 Bad Request error when a resource does not exist. After all, the client made a bad request, right?

HTTP error codes in the 4xx range all indicate that client made a mistake. However, when a client makes a request to a resource that does not exist, the server should respond with a 404 Not Found error.

404 Not Found is a more descriptive status code to use because it tells the client that they are trying to access a resource that does not exist.

Bad Request (400) vs Not Found (404)

The main difference between a "400 Bad Request" and "404 Not Found" status codes is that Bad Request indicates incorrect syntax, while Not Found indicates a missing resource. Moreover, bad requests should not be repeated, while not found requests can be repeated. The "404 Not Found" responses are cacheable, while "400 Bad Request" are not.

A 400 Bad Request error indicates that the server was unable to understand the request. This can be caused by incorrect syntax or invalid characters in the URL.

A 404 Not Found error means that the server was unable to find the requested resource. This can happen if the URL is typed incorrectly, or if the page has been deleted.

While both of these errors can be frustrating, a 404 Not Found error is generally more forgiving because it means that the resource could not be found. In contrast, a 400 Bad Request error suggests that there is something wrong with the request itself, which may require further investigation.

RFC7231 6.5.1 Explaining 400 Bad Request
RFC7231 6.5.1

Why we shouldn't repeat requests that return the 400 status code?

We shouldn't repeat requests that return "400 Bad Requests" errors because that error indicates incorrect syntax. So no matter how many times we repeat the request, the result will be the same: a 400 Bad Request error. In addition, repeating a request can also cause performance problems for the server, so it's best to avoid doing it.

On the other hand, you can repeat requests that return "404 Not Found" because the resource might become available. Because of the possibility that resource becomes available, the "404 Not Found" responses are cacheable.

See also:

Published on
Download Free Software Developer Career Guide

I've used these principles to increase my earnings by 63% in two years. So can you.

Dive into my 7 actionable steps to elevate your career.