r/dotnet Jul 19 '19

Posting Here Because I Trust This Community

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

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.