对禁用资源的操作的预期 HTTP 状态代码

Expected HTTP status code for an action on a disabled resource

给定以下条件时,expected/proper 操作的 HTTP 状态代码是什么:

资源当前被禁用,因此无法完成操作并获得预期结果。用户可以更改资源的状态并重试相同的请求。响应正文中将包含有关资源无法完成请求的原因的信息。

我的想法是 409 Conflict 将是最好的响应,因为用户可能会更改资源的状态并重新提交请求,但也许有更好的东西可以指示 "This method is generally allowed by you, but the resource is currently in a state that prevents it from completing as expected."

409 似乎最合适,这就是 http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html 所说的:

This code is only allowed in situations where it is expected that the user might be able to resolve the conflict and resubmit the request. The response body SHOULD include enough information for the user to recognize the source of the conflict. Ideally, the response entity would include enough information for the user or user agent to fix the problem; however, that might not be possible and is not required.

看来这是网上的共识,我看不出更好的here. See here for another similar question

409

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

然后跟进

Conflicts are most likely to occur in response to a PUT request. For example, if versioning were being used and the representation being PUT included changes to a resource that conflict with those made by an earlier (third-party) request

这似乎更像是在客户端构建调用时实体在客户端下方移动,也许假设是客户端在进行调用之前要求允许的操作。如果您实现了它,我会说 409 对您的 API 完全有效,因为您为客户提供了 有效请求的能力,除非其他人更改了实体。

显然您应该保持一致,并记录响应代码及其用法。

根据 RFC 4918 (https://www.rfc-editor.org/rfc/rfc4918#section-11.2),您可以使用代码 422(不可处理的实体)来处理类似的情况。

422 Unprocessable Entity

The 422 (Unprocessable Entity) status code means the server
understands the content type of the request entity (hence a
415(Unsupported Media Type) status code is inappropriate), and the
syntax of the request entity is correct (thus a 400 (Bad Request)
status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML
request body contains well-formed (i.e., syntactically correct), but
semantically erroneous, XML instructions.