3

Let's say I have a REST POST end-point: www.foo.com/id/X where X represents a number.

My server-side pseudocode looks like this:

performIdLookup(int id) { 

  if ( idExists(id) ) {
     return toJson(lookup(id)) // returns 200/OK Status Code with object as JSON
  }
  else {
     return HTTP_???_error 
  }
}

Per this question, sending a 400 error doesn't seem right here since the user submitted a valid request, but the server couldn't locate it.

What's the right HTTP response here and why?

6
  • that is the right error 404 Commented Jul 11, 2014 at 17:38
  • you should have 404 memorized by now - doing web development ;p Commented Jul 11, 2014 at 17:39
  • 404 is ok. by the way 500 is internal server error Commented Jul 11, 2014 at 17:40
  • @ScottSelby 500 should not be non-authenticated. 500 Internal Server Error: " A generic error message, given when an unexpected condition was encountered and no more specific message is suitable." Commented Jul 11, 2014 at 17:41
  • @ScottSelby Since the response is JSON, this looks like a REST API. Redirecting is not a good idea for such a scenario. Commented Jul 11, 2014 at 18:04

2 Answers 2

5

That is very easy.

404 Not Found

If there is no resourece at /id/42, a resource can not be found for this URL.

See the list of HTTP status codes.

Sign up to request clarification or add additional context in comments.

3 Comments

404 The server has not found anything matching the Request-URI I don't understand your answer wrt to this text. Is it because there's no resource living at an id of X?
@KevinMeredith - you wrote " server couldn't locate it." that is exactly what 404 is for
The server has not found anything matching the Request-UR(Resource)I(Identifier).
1

Not 400 (bad request). But 404 (not found). Yes, 404 is not what we are used to watching in these cases, but you can add some custom information with response.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.