Question

I am working on making a rest api and I have a question about what is typically returned from a get with no match. For instance if my resource is "users" and I make an api call with

http method: GET url: api.mysite.com/users/123

If '123' exists I will return a http code of 200 with the details of the user in the response body. My question is; what http code should I return, and what should I put in the response body if there is no user with the id of 123?

Should I just return a code of 200 and an empty body?

Was it helpful?

Solution

Use 404, because requested resource (User) not exist.

10.4.5 404 Not Found

The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable. RFC 2616

OTHER TIPS

As its your API, so you can return whatever you want but if you talked about the standard of HTTP responses than I would suggest you to return 404 (NOT FOUND) in that case which looks more suitable in this case. Because the resource you are trying to access is not available at that location.

404- Not Found implies the server has not found anything matching the Request-URI.

With status code 404 you can also provide the response body or any other meaningful header with proper error message. So that client can understand it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top