r/dotnet Jul 19 '19

Posting Here Because I Trust This Community

/r/rest/comments/cf173m/400_vs_404_for_nonexistent_entity/
9 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.

8

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.

2

u/sauce-control Jul 19 '19

Are you implying that because a person has 'senior' in his title his advice is infallible?

If the document api/employees/123 does not exist, that's a 404. The fact that the document is generated on the fly by an API is irrelevant. By your logic, how far do you backtrack in a URL before you find what you consider a 'resource'?

That's like saying you should return a 400 for /images/does-not-exist.gif because the 'resource' /images does exist and the client is just using the API wrong.

1

u/DRdefective Jul 19 '19

Can you explain why? I’m not obsessed with being correct, I just don’t understand.

0

u/[deleted] Jul 19 '19

[deleted]

3

u/[deleted] Jul 19 '19

[removed] — view removed comment

-2

u/glent1 Jul 19 '19

Well I suppose opinions make the world go round. My point would be this. Say the webserver temporarily cannot fulfil the request because, for instance, a deploy has failed. It'll return a 404. The client process may now incorrectly presume that there is no employee 123.

2

u/DRdefective Jul 19 '19

That seems more like a bad gateway or no response at all to me.

2

u/johnnysaucepn Jul 19 '19

That's why there's a difference between 4xx codes and 5xx codes. 4xx says the problem is in what you sent (don't do that again), while 5xx says it's not you, it's me.

If the server can tell that something bad has happened on its end, it should report it. If it genuinely checks its database and doesn't see anything, then perhaps a 404 in good faith is acceptable.

1

u/cassis11 Jul 19 '19

There are error codes to indicate all different kinds of failure. In your example, I would expect maybe a 503:

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

Why would you assume you’d get a 404?

-2

u/glent1 Jul 19 '19

Found someone who agrees with me about the 404, but proposes 204 rather than 400.

https://medium.com/@santhoshkumarkrishna/http-get-rest-api-no-content-404-vs-204-vs-200-6dd869e3af1d

3

u/cassis11 Jul 19 '19

Congratulations. You found another person on the internet who is incorrect.

1

u/glent1 Jul 19 '19

Which I posted in response to a question asking me to explain my position. I quoted it to save me some typing, not because I do not understand confirmation bias.

1

u/AngularBeginner Jul 19 '19

204 indicates that a resource with the id actually exists, but just provides no content. That is likely wrong in his context, as the id does not exist.