简体   繁体   中英

HTTP status code for a REST search without result

I have a REST endpoint for a search.

GET /person?firstname=john&name=smith

As result a collection with the HTTP status code 200 OK is returned:

[
   {
      "id":11,
      "firstname":"John",
      "name":"Smith",
      "birthday":"1996-03-08"
   },
   {
      "id":18,
      "firstname":"John",
      "name":"Smith",
      "birthday":"1963-07-11"
   }
]

What is the correct HTTP status code and payload for a empty search result?

  • HTTP Status 200 OK with a empty collection []

  • HTTP Status 204 No Content with a empty collection []

  • HTTP Status 204 No Content with a empty body

IMO the first case:

  • HTTP Status 200 OK with a empty collection []

Then you don't have to check for the 204 and use what is returned, which is an empty list.

I would recommend:

HTTP Status 200 OK with a empty collection []

The reason is that 204 means there is no content to send back and that is not what the result of your call is.

In your case, there IS something to send back. It's a Json result that happens to be composed of an empty collection.

UPDATE:

From the HTTP protocol definition:

10.2.5 204 No Content: The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation.

And:

The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.

This is a matter of convention here when you are designing your web application. 200 ok with an Empty collection would be my way to go . Because :

  • It proved that your API does return response
  • Handling 200 would be much easier on client-side , as I may assume you are working with Javascript , where other response code might throw you an error

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