REST API Status Code for user not exist

Viewed 8823

Status code for object not found.

I'm making GET users/57 REST API call and this user(id=57) doesn't exist. Shall I use 404 Status or what can be the status code used for this

3 Answers

As your question is written ("GET users/57") and that user doesn't exist then YES, the correct response is 404.

You seem to be modeling a REST API and "user" is just another resource. This is unrelated to authentication you are just making a GET on a domain and the resource doesn't exist, so 404 is the right answer.

Even if this is part of an authentication process (getting userdetails) the correct answer for THAT request is 404 that will be transformed in 401 at some point by security interceptor (e.g. throwing UsernameNotFoundException that extends AuthenticationException as a consequence of that 404)

I would use error code 401 for unknown user, and not 404. 404 means the resource (IE the API endpoint) was not found.

401: Either you need to provide authentication credentials, or the credentials provided aren't valid.

Related