rest - 资源已存在时 POST 的 HTTP 响应代码

标签 rest http http-status-codes

我正在构建一个允许客户端存储对象的服务器。这些对象完全在客户端构建,并带有在对象的整个生命周期内永久存在的对象 ID。

我已经定义了 API,以便客户端可以使用 PUT 创建或修改对象:

PUT /objects/{id} HTTP/1.1
...

{json representation of the object}

{id} 是对象 ID,因此它是 Request-URI 的一部分。

现在,我也在考虑允许客户使用 POST 创建对象:

POST /objects/ HTTP/1.1
...

{json representation of the object, including ID}

由于 POST 意味着“追加”操作,我不确定如果对象已经存在该怎么做。我应该将请求视为修改请求还是应该返回一些错误代码(哪个)?

最佳答案

我的感觉是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.

关于rest - 资源已存在时 POST 的 HTTP 响应代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3825990/

相关文章:

java - 授权 header 中不属于用户的 token 的 HTTP 状态代码

web-services - 命令模式是否与 RESTful Web 服务兼容?

ios - Firebase REST API - 如何观察和监听变化

api - REST Api 的自动化测试

regex - Go中的http Get请求(获取几行)

http - SSL session 票证

逻辑错误的 HTTP 400(错误请求),而不是格式错误的请求语法

RESTful 实现和一般信息

java - Jsoup:尝试访问页面时出现错误 307

http - 未激活帐户的 HTTP 状态代码是什么?