哪个HTTP代码用于REST API中的空子资源?

Tre*_*ent 16 collections rest http-status-codes semantics

比方说,我有一个资源articles/articles.

这些文章可能有相关的文章,所以我通过GETting获取它们/articles/{id}/related.

我应该返回的是没有相关文章?

我能想到:

  • 404 Not Found,也许是一个空的集合
  • 204 No Content
  • 200 Found 一个空的集合

有什么建议吗?(请提出论点)

顺便说一句,它可能适用于分页.如果我请求2的第3页,那么第3页将返回一个空集,如果它是404

小智 15

404不是你想要的.这是一个错误的条件.你的情况不是错误.客户目前不知道是否有任何相关文章并想知道.那不是错误.

204也不合适.RFC 2616规定:

  The server has fulfilled the request but does not need to return an
  entity-body, and might want to return updated metainformation. The
  response MAY include new or updated metainformation in the form of
  entity-headers, which if present SHOULD be associated with the
  requested variant.
Run Code Online (Sandbox Code Playgroud)

204没有指明没有相关文章.它只是说服务器不需要发送数据.

200 另一方面,空集合将满足您的需求.


Hen*_*olm 6

我不会用404- 这会告诉客户"我无法告诉你是否有任何相关的文章".如果根本不识别{id},那将是合适的.你想要的是一个积极的回应告诉客户,是的,好的问题,这里是相关文章的(空)列表.

204由于相关原因,也不好.它指出没有答案,这仍然是来自一个答案,不同的有,但恰好是空列表.它的描述POST比a 更有意义GET.

200 有一个空列表是正确的.