The correct HTTP status code for an existing resource, but a non-existent object?

Suppose the client requests the following URL:

/user-details?user=123 

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

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

  • I still returned 404 Not Found , but experience told me that it is confusing not to know if it is a resource or an entity that was not found;
  • I reviewed the use of 400 Bad Request , but I find it confusing, since the request is technically correct, just requesting a nonexistent object.

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

+8
source share
4 answers

404 is excellent because a user data resource is a conceptual mapping to a user object in this case with partial information about user resources.

The GET method for custom parts is not responsible for differentiating from two cases: a) The user does not exist, b) The user information does not exist.

I would rewrite the endpoint something like this:

 /user/123/details 

Which, in my opinion, is more expressive.

+4
source

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

The request component contains non-hierarchical data, which together with the data in the path component (section 3.3) serve to identify the resource within the URI scheme and naming authority

Therefore, 404/Not found excellent.

+3
source

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

For me, the status 404 is also normal (better normalized actually), 400 is too vague.

+2
source

While technically 404 is just fine, from a user’s point of view, I return to 200 and provide a more user-friendly message to say that the user tried to access an object that is not an entity.

-3
source

All Articles