简体   繁体   English

没有结果的REST搜索的HTTP状态代码

[英]HTTP status code for a REST search without result

I have a REST endpoint for a search. 我有一个用于搜索的REST端点。

GET /person?firstname=john&name=smith

As result a collection with the HTTP status code 200 OK is returned: 结果,返回HTTP状态代码为200 OK的集合:

[
   {
      "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状态代码和有效负载是什么?

  • HTTP Status 200 OK with a empty collection [] HTTP状态200 OK ,集合为空[]

  • HTTP Status 204 No Content with a empty collection [] HTTP状态204 No Content且集合为空[]

  • HTTP Status 204 No Content with a empty body HTTP状态204 No Content为空的204 No Content

IMO the first case: IMO第一种情况:

  • HTTP Status 200 OK with a empty collection [] HTTP状态200 OK ,集合为空[]

Then you don't have to check for the 204 and use what is returned, which is an empty list. 然后,您不必检查204并使用返回的内容,这是一个空列表。

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. 原因是204表示没有内容可以发回,这与您的通话结果无关。

In your case, there IS something to send back. 在您的情况下,有些东西可以退回。 It's a Json result that happens to be composed of an empty collection. 这是一个Json结果,恰好由一个空集合组成。

UPDATE: 更新:

From the HTTP protocol definition: 根据HTTP协议定义:

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. 10.2.5 204无内容:服务器已完成请求,但不需要返回实体,可能要返回更新的元信息。

And: 和:

The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields. 204响应必须不包含消息正文,因此始终由标头字段之后的第一个空行终止。

This is a matter of convention here when you are designing your web application. 在设计Web应用程序时,这是一个惯例问题。 200 ok with an Empty collection would be my way to go . 我要走200英镑,带空的收藏夹。 Because : 因为:

  • It proved that your API does return response 证明您的API确实会返回响应
  • 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 在客户端处理200会容易得多,因为我假设您正在使用Javascript,而其他响应代码可能会向您抛出错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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