REST:哪个 HTTP 状态用于拒绝当前资源状态的方法

REST: which HTTP Status for rejecting method on current state of resource

想象一下以下场景:

客户端想要通过 PUT 更改资源状态(例如 Order)。 此资源允许该方法,但最终状态更改为 orderState=payed 需要一些先决条件(例如 POST 将有效的支付资源添加到订单中)。 因此只有当目标资源处于特定状态时才允许 PUT。

什么是合适的响应代码?

我认为 405 - Method Not Allowed 不太适合这种情况,因为它意味着客户端根本无法对该资源使用 PUT。

我目前正在考虑 409、423、424 和 428,但我想知道是否有关于此特定场景的一些最佳实践。

我希望 405 Method Not Allowed 涵盖您的案例:

The 405 (Method Not Allowed) status code indicates that the method received in the request-line is known by the origin server but not supported by the target resource. The origin server MUST generate an Allow header field in a 405 response containing a list of the target resource's currently supported methods.

我的理解是“当前支持”意味着支持的方法列表可能会随着时间的推移而变化。

403 Forbidden也可以:

The 403 (Forbidden) status code indicates that the server understood the request but refuses to authorize it.... a request might be forbidden for reasons unrelated to the credentials.

与任何 4xx class 状态代码一样,消息正文应包含“包含对错误情况的解释的表示”,这是您插入问题解释的地方。