0

So let's say I have a WebApi controller called UsersController. Let's look at the following examples:

1.Navigating to /Users/1 returns JSON of a user with Id = 1. HTTP response code will be 200.

2.Navigating to /User/1 (note I misspelled the URL!) will return response code 404. I do not even need to do anything, my web server will return code 404 for me.

Now the question: what response code (200 or 404) should be returned by the URL /Users/2, if user with Id = 2 does not exist in the database? And why.

1 Answer 1

1

You should return NotFound (404), because the url is valid but the required resource doesn't exists. check this.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Omar, I expected to get a reply like this. Let's make this a bit more interesting: - WebApi also allows to edit users via PUT. - Users belong to Teams. - I'm trying to edit UserId = 1, moving him from TeamId = 5 to TeamId = 6. Now we can have 3 scenarios: 1. Consumer of the API requested the wrong URL 2. UserId = 1 has been deleted 3. New scenario: TeamId = 6 has been deleted Should I be returning 404 for the 3rd scenario? It seems much more logical to return 404 for "web resource not found", and 200 for any handled scenario like "User was deleted" or "Team was deleted"
For scenario 3, It will be endless cases that you have to check and investigate, because if this user has an address or any other entity attached to it, you also need to check the existence of these rows like (AddressId = 10), so what I will do is to 1. implement the concurrency with my user table 2. let the database save fails because TeamId 6 will not be there and saving the foreign key will fail because of it.
Agree with everything you said - the question is how does that translate to decisionmaking about response codes? To me it looks more logical to always return 200 if my code handled the non-standard scenario (User not found or Team not found or whatever else not found), and let my web server return 404 for cases where web resource requested by the consumer was not found on the web server, which has nothing to do with my business logic.
Definitely you have to handle the exceptions and errors that you encounter on the server side and wrap that with an object that has for example a State and List of Errors that you can send back to the client, so if you encounter concurrency problem for example you can send the state as error and send some error message tells the client why the request failed, and if it was successful send the state as SUCCESS, but in both cases, you can return 200 as the status code. Hope that helps.
Again, how does what you're saying translate to HTTP response codes? E.g. I'd like to return 200 for every scenario which I handle in my business logic, and only return codes like 404 and 500 for correspondingly not found web resources and unhandled exceptions.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.