28

Say the client is requesting the following URL:

/user-details?user=123

If /user-details was a non-existing resource, the correct status code would obviously be 404.

However if /user-details does exist, but no user with id 123 exists:

  • I've so far returned a 404 Not Found, but experience has told me that it makes it confusing to not know whether it is the resource, or the entity that was not found;
  • I've considered using 400 Bad Request, but I find it confusing as well, as the request is technically correct, just requesting a non-existing entity.

Is there a more suitable HTTP status code for this purpose?

3
  • Is there a reason you're not comfortable accepting one of the current answers? What did you end up using? Commented Mar 7, 2018 at 13:36
  • After reading the answers below, I think that 404 is better, I'm just not ready to accept it yet, I believe ;) Commented Mar 7, 2018 at 17:48
  • http should just extend their specification to include new code, so that we could differentiate between 404-resource-not-found and 404-requested-entity(resource) -not-found. 404-1 or 404-a or whatever works. Commented Dec 5, 2018 at 13:00

3 Answers 3

14

The 404 is fine because the user-details resource is a conceptual mapping to the user entity in this case to a partial user resource information.

The GET method for user-details is therefore not responsible for differentiating from the two cases: a) The user doesn't exist, b) The user details don't exist.

I would however rewrite the endpoint to something like this:

/user/123/details

Which in my opinion is more expressive.

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

Comments

12

Try 422 which is used in WebDav? See http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

For me 404 status is ok too (better normed actually), 400 is too vague.

1 Comment

Interesting argument in favor of this answer here: "Specifically, many APIs, like Twitter and Recurly, are using the status code "422 Unprocessable Entity" as defined in the HTTP extension for WebDAV."
5

The user parameter is part of the resource identifier as stated in RFC 3986, section 3.4:

The query component contains non-hierarchical data that, along with data in the path component (Section 3.3), serves to identify a resource within the scope of the URI's scheme and naming authority

Hence, 404/Not found is perfectly fine.

4 Comments

So the fact that it's part of the query string instead of the path does not matter? Do you have any link to some resource backing this up?
I've updated my answer to contain a quote from the relevant RFC.
From RESTful perspective (If it was a REST API) query strings are used for refining the result of the operation, which is consistent with the use of the question mark. It should be used for services which URI path maps to a set of entities instead of a single one, e.g. /users?name=John. You will normally see the URIs as a path identifying a resource like /users/123 which is a synonym of /user/123. Keep in mind that several frameworks use this approach for mapping requests, so using query string for identifying resources will be problematic in that sense.
@raspacorp I see your point and would even agree to you as far as addressing resources in ReST is concerned. However, I think you are missing an important trait of URLs: They are references and their identity is not to be confused with the identity of referenced resources. After all, they are uniform, not unique resource locators. Oh and by the way: In my experience, the relations expressed through ReSTful paths is rather ambiguous. It could be either 'has-a' or 'is-subset-of.' Later one fitting perfectly into the refining or result operations you mentioned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.