成功但为空的主体使用什么状态代码?

What Status Code to use for Successful but null body?

我的团队和我自己正在尝试决定使用什么状态代码,我们的 API 通过 Swagger 生成的客户端有一个 204 (No Content) 的条件,它比 404 更有意义在这种情况下。

我们想显示响应成功但是没有Object返回。

假设这是一个购物车: 客户点击购物车并将您带到页面,请求发送到端点 Get//CartOrderDetails 但购物车中没有商品,我们不希望它通过 [=28] 运行 =] 客户端的 200 路径,但我们也不希望它通过 404 路径,因为它不是 Not Found.

在这种情况下你们会用什么?

编辑:感谢大家一夜之间的回复!绝对有助于让事情变得清晰,似乎总会有支持和反对 204 的争论。谢谢大家!

What would you guys use in this case?

we don't want it to run through the API Client's 200 path

对于 REST,这是错误的想法。

Uniform interface 是一个重要的 REST 约束。除其他外,这意味着所有资源都使用具有相同语义的自描述消息。

The status-code element is a 3-digit integer code describing the result of the server's attempt to understand and satisfy the client's corresponding request. The rest of the response message is to be interpreted in light of the semantics defined for that status code. -- RFC 7230

换句话说,状态码是transfer of documents over a network domain的元数据。您应该根据响应消息的语义选择状态代码,而不是根据您希望如何处理该消息。

如果您发送回客户端的是所请求资源的表示,那么您应该返回 200。

如果资源的表示恰好是零字节长,您可能发送 204 而不是 200,但这并不是它的真正用途。 204center a corner case from remote authoring.

的语义

当然,如果您的响应包含正文,那么使用 204 状态码很可能会造成混乱;所以不要那样做。

200 OK
Content-Type: application/json

null