简体   繁体   中英

HTTP Response for Unsuccessful Handling of Request

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?

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM