HTTP response code for POST when resource already exists
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.
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}
The {id} is the object ID, so it is part of the Request-URI.
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}
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)?
My feeling is 409 Conflict
is the most appropriate, however, seldom seen in the wild of course:
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.
Personally I go with the WebDAV extension 422 Unprocessable Entity
.
REST Patterns describes it as
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.
根据RFC 7231,a 303可以使用如果处理POST的结果等同于现有资源的表示。
链接地址: http://www.djcxy.com/p/1082.html上一篇: 创建资源时使用单数或复数名称的资源