HTTP Status 204 vs 404

Thiago Bomfim
2 min readFeb 7, 2024

--

The HTTP Status 204 means that it’s a successful request, but doesn’t have a response body, while the 404 means the resource doesn’t exist.

In the RFC5789 — HTTP PATCH we find that:

Resource not found: Can be specified with a 404 (Not Found) status code when the client attempted to apply a patch document to a non-existent resource, but the patch document chosen cannot be applied to a non-existent resource.

In the book REST API — Design Rulebook by Mark Masse we find that:

Once a DELETE request has been processed for a given resource, the resource can no longer be found by clients. Therefore, any future attempt to retrieve the resource’s state representation, using either GET or HEAD, must result in a 404 (“Not Found”) status returned by the API.

The Azure API has this recommendation:

“DO return a 204-No Content without a resource/body for a DELETE operation (even if the URL identifies a resource that does not exist; do not return 404-Not Found)”

Conclusion

The 204 is usually used when there is no content to return, when the resource is not found usually the 404 should be returned. Usually, the 404 is not used for the DELETE operation, as it is an idempotent method. However, it is used in the case of PATCH, GET, or HEAD operations.

In the end, the most important thing is to define a guideline and align it with the team that will use it. The key here is having a common pattern and sticking to it.

If you are interested in staying up-to-date with software development-related news, please subscribe to our newsletter: https://devjava.substack.com/

References

REST API — Design Rulebook — Mark Masse

AZURE API Guidelines

--

--

Thiago Bomfim
Thiago Bomfim

Written by Thiago Bomfim

I'm a happy developer, trying to help the world and peoples with technologic

No responses yet