Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST standard for GET on a resource that doesn't exist

Tags:

rest

The resource /user/12345 doesn't exist. Lets say the consumer is trying different ids randomly. There is no authorization. Any user can view any user. In a broader sense, my question is "What should you return if you do a GET on a resource that doesn't exist?"

Should I return an empty user for an id that doesn't exist or should I return an error message with proper status code?

What is the typical/usual/recommended practice?

like image 646
Langali Avatar asked Oct 07 '09 18:10

Langali


People also ask

What happens when you send PUT request but the resource does not exist?

If the target resource does not have a current representation and the PUT successfully creates one, then the origin server MUST inform the user agent by sending a 201 (Created) response.

Should a non existent page always return 404?

When a user requests a nonexistent URL on your website, you should return an individual error page that lets them know that the requested URL does not exist. You should also make sure that the server returns the correct HTTP status code “404“.

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.

What code would you use if a resource used to exist but no longer does?

The HyperText Transfer Protocol (HTTP) 410 Gone client error response code indicates that access to the target resource is no longer available at the origin server and that this condition is likely to be permanent.


2 Answers

Return 404 status code.

like image 175
Byron Whitlock Avatar answered Sep 18 '22 15:09

Byron Whitlock


It depends on your security concerns a little bit. I would either send a 404 if it is OK that the guesser finds out if that user id does not exist, or send 401 for all attempts on unauthenticated accesses to any resource under /user

like image 38
stinkymatt Avatar answered Sep 17 '22 15:09

stinkymatt