Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

http - 资源已存在时POST的HTTP响应代码(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.

(这些对象在客户端完全构造,完整的对象ID对于对象的整个生命周期是永久的。)

I have defined the API so that clients can create or modify objects using PUT:

(我已经定义了API,以便客户端可以使用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.

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

Now, I'm also considering allowing clients to create the object using POST:

(现在,我也在考虑允许客户端使用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.

(由于POST意味着“追加”操作,我不知道如果对象已经存在该怎么办。)

Should I treat the request as modification request or should I return some error code (which)?

(我应该将请求视为修改请求还是应该返回一些错误代码(哪个)?)

  ask by vmj translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My feeling is 409 Conflict is the most appropriate, however, seldom seen in the wild of course:

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

(冲突最有可能发生在响应PUT请求时。)

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.

(例如,如果正在使用版本控制并且包含PUT的实体更改为与早期(第三方)请求所产生的资源冲突的资源,则服务器可能会使用409响应来指示它无法完成请求。)

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.

(在这种情况下,响应实体可能包含由响应Content-Type定义的格式的两个版本之间的差异列表。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...