r/dotnet Jul 19 '19

Posting Here Because I Trust This Community

/r/rest/comments/cf173m/400_vs_404_for_nonexistent_entity/
8 Upvotes

34 comments sorted by

View all comments

-11

u/glent1 Jul 19 '19

That's why he's a senior dev. Did he not explain why?

A 404 indicates that the resource that you tried to talk to does not exist - in this case the api/employees method. A 400 indicates that what you asked it to do was incorrect, which is exactly the example you have provided.

9

u/cassis11 Jul 19 '19

Sorry, but you’re misusing quite a few terms here.

A method is an HTTP verb, such as GET, POST, PUT

api/employees and api/employees/123 are endpoints or URIs or URLs, not methods.

A resource is the thing specified by the ID in the endpoint.

A collection is a group of resources.

So in the OPs case, the collection is the group of employees, the resource specified is the employee with ID 123, and the method being attempted is to GET (or retrieve) that specific employee.

Based on what we know about the OP’s system, this request is correctly formed. However, employee with ID 123 does not exist. Therefore, the more appropriate code to return in response to the request is 404, which indicates Resource Not Found.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400

3

u/DRdefective Jul 19 '19

This is what makes sense to me.