当记录处于不适当的状态时,什么 HTTP 代码 return?

What HTTP code return when record is in inappropriate state?

我需要将可通过 REST Api 访问的特定资源设置为定义的状态之一 - 空闲、运行 或停止。如何通知 REST API 客户端记录已处于请求状态?例如。客户想要开始记录并且该记录已经 运行?他需要知道记录是 运行 并且操作不可处理。

这绝对是客户端错误,因此来自 4xx 系列的状态代码应该是答案。我有使用 400409412 的想法,但不确定。

How to inform a REST API client that record is already in requested state

我能找到的最接近的是

409 Conflict

The request could not be completed due to a conflict with the current state of the resource. 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.

Conflicts are most likely to occur in response to a PUT request. For example, if versioning were being used and the entity being PUT included changes to a resource which conflict with those made by an earlier (third-party) request, the server might use the 409 response to indicate that it can't complete the request. In this case, the response entity would likely contain a list of the differences between the two versions in a format defined by the response Content-Type.

强调我的

RFC 2616

这里有 "look-before-you-leap" 个技巧可能有用。

您可以在您的 PUT(我假设是 PUT)请求中插入 If-Match header

PUT /states HTTP/1.1
Host: www.example.com
Content-Type: text/plain
If-Match: "running"

Running

你得到 200412 (Precondition Failed)