无结果的 REST 搜索的 HTTP 状态代码
HTTP status code for a REST search without result
我有一个用于搜索的 REST 端点。
GET /person?firstname=john&name=smith
作为结果,返回了一个具有 HTTP 状态代码 200 OK
的集合:
[
{
"id":11,
"firstname":"John",
"name":"Smith",
"birthday":"1996-03-08"
},
{
"id":18,
"firstname":"John",
"name":"Smith",
"birthday":"1963-07-11"
}
]
空搜索结果的正确 HTTP 状态代码和负载是什么?
HTTP 状态 200 OK
,集合为空 []
HTTP 状态 204 No Content
有一个空集合 []
HTTP 状态 204 No Content
正文为空
IMO 第一种情况:
- HTTP 状态
200 OK
,集合为空 []
那么您就不必检查 204 并使用返回的内容,这是一个空列表。
我会推荐:
HTTP Status 200 OK with a empty collection []
原因是204表示没有内容要发回,这不是你调用的结果。
在你的情况下,有些东西要寄回。这是一个 Json 结果,恰好由一个空集合组成。
更新:
来自 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.
并且:
The 204 response MUST NOT include a message-body, and thus is always
terminated by the first empty line after the header fields.
这是设计 Web 应用程序时的惯例问题。 200 可以使用空集合 是我的选择。因为:
- 证明你的API确实return回应了
- 在客户端处理 200 会容易得多,因为我假设您正在使用 Javascript,其他响应代码可能会给您带来错误
我有一个用于搜索的 REST 端点。
GET /person?firstname=john&name=smith
作为结果,返回了一个具有 HTTP 状态代码 200 OK
的集合:
[
{
"id":11,
"firstname":"John",
"name":"Smith",
"birthday":"1996-03-08"
},
{
"id":18,
"firstname":"John",
"name":"Smith",
"birthday":"1963-07-11"
}
]
空搜索结果的正确 HTTP 状态代码和负载是什么?
HTTP 状态
200 OK
,集合为空[]
HTTP 状态
204 No Content
有一个空集合[]
HTTP 状态
204 No Content
正文为空
IMO 第一种情况:
- HTTP 状态
200 OK
,集合为空[]
那么您就不必检查 204 并使用返回的内容,这是一个空列表。
我会推荐:
HTTP Status 200 OK with a empty collection []
原因是204表示没有内容要发回,这不是你调用的结果。
在你的情况下,有些东西要寄回。这是一个 Json 结果,恰好由一个空集合组成。
更新:
来自 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.
并且:
The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.
这是设计 Web 应用程序时的惯例问题。 200 可以使用空集合 是我的选择。因为:
- 证明你的API确实return回应了
- 在客户端处理 200 会容易得多,因为我假设您正在使用 Javascript,其他响应代码可能会给您带来错误