没有结果的 REST 搜索的 HTTP 状态代码 [英] HTTP status code for a REST search without result

查看:32
本文介绍了没有结果的 REST 搜索的 HTTP 状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于搜索的 REST 端点.

GET/person?firstname=john&name=smith

结果是一个带有 HTTP 状态代码 200 OK 的集合被返回:

<预><代码>[{身份证":11,"firstname":"约翰","name":"史密斯","生日":"1996-03-08"},{身份证":18,"firstname":"约翰","name":"史密斯","生日":"1963-07-11"}]

空搜索结果的正确 HTTP 状态代码和有效负载是什么?

  • HTTP 状态 200 OK 带有空集合 []

  • HTTP 状态 204 No Content 带有空集合 []

  • HTTP 状态 204 No Content 空体

解决方案

我建议:

HTTP Status 200 OK with a empty collection []

原因是204意味着没有内容要发回,这不是你调用的结果.

在你的情况下,有东西要送回去.这是一个 Json 结果,恰好由一个空集合组成.

更新:

来自 HTTP 协议定义:

<块引用>

10.2.5 204 无内容:服务器已完成请求但未执行不需要返回实体主体,并且可能想要返回更新元信息.

还有:

<块引用>

204 响应不能包含消息体,因此总是由头字段后的第一个空行终止.

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

解决方案

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.

这篇关于没有结果的 REST 搜索的 HTTP 状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆