REST:资源处于错误状态——我应该 return 哪个 HTTP 状态?

REST: Resource in wrong state -- Which HTTP status should I return?

假设我的服务器公开了具有状态 (Not approvedApprovedAuto-approved) 的资源。如果资源处于 Not approved 状态,则客户端不应访问它,即允许客户端知道该资源的存在(无论如何它们都知道),但在资源处于正确状态之前应拒绝访问。

HTTP 状态代码应该表达如下内容:“是的,您尝试访问的资源存在,但您必须等待,直到它被某人批准。请稍后再试。”

在这种情况下应该返回什么 HTTP 状态代码?返回 404 (Not found) 似乎不对,因为资源实际上是存在的。状态代码 412 (Precondition failed) 听起来不错,但是 the RFC 谈论了很多关于 HTTP headers...

The 412 (Precondition Failed) status code indicates that one or more conditions given in the request header fields evaluated to false when tested on the server. This response code allows the client to place preconditions on the current resource state (its current representations and metadata) and, thus, prevent the request method from being applied if the target resource is in an unexpected state.

描述中提到的条件是指HTTP headers like If-Match, If-Modified-Since, If-None-Match, If-Range or If-Unmodified-Since so 412 的使用不适合您的情况。

我认为如果 Not approved 资源根本不可用 404、仅限于某些用户 403 或锁定 423

,这取决于您的解释

我会说在你的情况下 423 (Locked)(直到批准)将是正确的回应。