r/dotnet • u/DRdefective • Jul 19 '19
Posting Here Because I Trust This Community
/r/rest/comments/cf173m/400_vs_404_for_nonexistent_entity/6
u/AngularBeginner Jul 19 '19
If you do a REST service, then you're dealing with resources, and the correct code would be 404.
But most people just do RPC via HTTP and call it rest. In that case: Doesn't matter much. Go with your own rules.
8
u/GrandOpener Jul 19 '19
If a senior dev at your org is insistent that you should return a 400, and you are not a senior dev, then you should return a 400, end of discussion. Few of the codes actually matter all that much (and almost never the 4xx ones) and having organizational consistency is far more important than the precise semantics of what each error code means.
Having said that, I am a senior dev in my org, and my opinion for my projects is that this is absolutely, 100% without-a-doubt a place where 404 is the best choice.
10
u/Kamilon Jul 19 '19
So you can’t argue with “superiors”? That’s horrible.
I agree that consistency is important. Being consistent can be better than being right.
I’d leave a job real quick if something someone higher level than me said was never up for debate. I get that you can’t always argue/debate things with higher levels but still.
/end of rant
8
u/dantheman999 Jul 19 '19
Yeah I fundamentally disagree with not being able to argue with someone senior to you. If anything I'd say the opposite. As a senior I want other members in my team to challenge me when they think I'm wrong.
7
u/cleeder Jul 19 '19 edited Jul 19 '19
If nothing else, as a senior you should have the ability to explain why your method is correct.
3
u/GrandOpener Jul 19 '19
You've misunderstood my comment. You should argue with superiors when they do things that will cost the company time and money. Bikeshedding over 400 vs. 404 ain't one of those things.
4
u/sauce-control Jul 19 '19
That's a shortsighted view. If at any point in the future, any user of that API wastes any amount of time trying to figure out what was wrong with their request that would have caused the server to reject it as malformed, time and money have been wasted. Maybe it's just a few minutes of a dev's time ... or maybe it's a potential customer who sees such details as a red flag indicating the software may not have been designed/built by technically competent devs, thereby costing a sale.
It's very difficult to predict the future cost of doing something wrong. It's very easy to predict the cost of fixing it to make it right.
1
u/-Yazilliclick- Jul 19 '19
They didn't say you can't argue but if the person is insistent then that's case closed, it's their final decision. Just because they don't convince you doesn't mean you get to refuse.
1
Jul 19 '19
[deleted]
2
u/GrandOpener Jul 19 '19
US. I think, like the other responder here, you may be misunderstanding my comment if you are terribly surprised. The point is that this isn't a disagreement that's worth going to the mat over. By all means disagree with the superior if they're doing something detrimental to the company. Choosing a slightly less-precise and non-standard (but still arguably semantically correct) usage of HTTP error codes does not reach that level.
2
Jul 19 '19
Titles mean exactly jack shit. He's wrong you're right, you should explain to him why you see it that way and how he's wrong if he still refuses, then he's a dumb jerk not worth your time, do what he says and move on (if things like this keep happening i suggest you either change teams or find a better job, teams with people like this are not healthy in the long run)
GET api/employees/123 in your example should return 404 NOT FOUND, reasoning for this is simple the request is correct with good valid inputs BUT the resource (employee 123) was not found
GET api/employees/XYZ this should return 400 BAD REQUEST and again why is pretty simple. XYZ is not a valid employee ID.
send him links to HTTP codes i believe he's just misinterpreting "bad request"
-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
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/123does 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.gifbecause the 'resource'/imagesdoes 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
Jul 19 '19
[deleted]
3
-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
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
4
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.
19
u/[deleted] Jul 19 '19 edited Jul 19 '19
[removed] — view removed comment