问题描述
我正在构建一个允许客户端存储对象的服务器。这些对象在客户端完全构造,完整的对象ID对于对象的整个生命周期是永久的。
I'm building a server that allows clients to store objects. Those objects are fully constructed at client side, complete with object IDs that are permanent for the whole lifetime of the object.
我已经定义了API,以便客户可以创建或使用PUT修改对象:
I have defined the API so that clients can create or modify objects using PUT:
PUT /objects/{id} HTTP/1.1
...
{json representation of the object}
{id}是对象ID,所以它是Request-URI的一部分。
The {id} is the object ID, so it is part of the Request-URI.
现在,我也在考虑允许客户使用POST创建对象:
Now, I'm also considering allowing clients to create the object using POST:
POST /objects/ HTTP/1.1
...
{json representation of the object, including ID}
由于POST意味着附加操作,我不知道如果对象已经存在该怎么办。我应该将请求视为修改请求还是应该返回一些错误代码(?)?
Since POST is meant as "append" operation, I'm not sure what to do in case the object is already there. Should I treat the request as modification request or should I return some error code (which)?
推荐答案
我的感觉是 409冲突
是最合适的,当然,在野外很少见到:
My feeling is 409 Conflict
is the most appropriate, however, seldom seen in the wild of course:
最有可能发生冲突以响应PUT请求。例如,如果正在使用版本控制并且包含PUT的实体更改为与早期(第三方)请求所产生的资源冲突的资源,则服务器可能会使用409响应来指示它无法完成请求。在这种情况下,响应实体可能包含由响应Content-Type定义的格式的两个版本之间的差异列表。
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.
这篇关于资源已存在时POST的HTTP响应代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!