PUT 只读实体的适当 HTTP 状态

Appropriate HTTP status for a PUTting a read-only entity

当客户端尝试 PUT当前只读 的实体时,适当的 HTTP 响应代码是什么?

玩具示例是产品发货。在发送货物之前,可以更改详细信息(地址、产品、数量)(例如使用 PUT 请求)。但是,一旦发送货物,任何 PUT 都应该失败,即使请求格式和语法是正确的。

有可能客户不知道货件已经发出,所以不是"careless"客户端的错误。

400 似乎不合适,因为输入格式正确且语法正确。
405 看起来很合适。在这种情况下,这是常见的反应吗?
403 似乎暗示授权已被撤销,这可能会产生误导。
422 似乎很合适,但它的用途似乎是 discouraged if you don't provide WebDAV capabilities(我们没有)。
500 听起来像是有人被电缆绊倒了,但我听说有人 developers/frameworks 在这种情况下使用此状态。

这种情况有标准做法吗?什么最不可能引起 API 用户(开发人员)和最终用户(使用 UI 的人)的混淆?

我会看看 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.

您的服务器完全理解请求,但不再支持写入。此外,return 客户端支持的方法列表的要求听起来很干净。

作为额外的好处,默认情况下 405 响应是可缓存的,这在您的情况下可能有意义。


另一个可行的选择是 409 Conflict:

The 409 (Conflict) status code indicates that the request could not be completed due to a conflict with the current state of the target resource.

可以说顺序改变了状态,以这种方式修改它不再是可能的。但请注意:

This code is used in situations where the user might be able to resolve the conflict and resubmit the request.

...所以我会倾向于另一个。