Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct HTTP status code for existent resource, but non-existent entity?

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?

like image 834
BenMorel Avatar asked Jan 04 '14 15:01

BenMorel


People also ask

What is the HTTP status for resource that doesn't exist?

404: “The requested resource was not found.” This is the most common error message of them all. This code means that the requested resource does not exist, and the server does not know if it ever existed.

What is HTTP status code 1xx?

A 1xx Informational status code means that the server has received the request and is continuing the process. A 1xx status code is purely temporary and is given while the request processing continues. For most tasks you won't encounter these much, as it's not the final response to the request. 100 Continue.

Which HTTP status code do you receive for a non existing page?

HTTP Status Code 410 - Gone A 410 is more permanent than a 404; it means that the page is gone. The page is no longer available from the server and no forwarding address has been set up.

When should I use HTTP 404?

If the server does not know, or has no facility to determine, whether or not the condition is permanent, the status code 404 (Not Found) SHOULD be used instead. This response is cacheable unless indicated otherwise.


3 Answers

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.

like image 145
raspacorp Avatar answered Oct 22 '22 03:10

raspacorp


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.

like image 42
cubitouch Avatar answered Oct 22 '22 05:10

cubitouch


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.

like image 40
DaSourcerer Avatar answered Oct 22 '22 03:10

DaSourcerer