我想知道人们对于返回空响应体的RESTful
PUT
操作有什么看法。HTTP规范(RFC 2616)有许多适用的建议。这是我的解释:
200 OK
。无需响应正文。(根据第9.6节,204 No Content
更为适用。)201 Created
,Location头字段返回新资源的最具体URI,响应正文中回显其他相关URI和元数据。(RFC 2616第10.2.2节)409 Conflict
,响应正文列出尝试更新和当前资源之间的差异。(RFC 2616第10.4.10节)400 Bad Request
,响应正文用自然语言文本(如英文)解释PUT失败原因。(RFC 2616第10.4节)注意:本答案撰写时RFC 2616是最新的规范,但该RFC已被取代。在引用任何标准时,验证您正在使用的是否为最新版本可能会很有用。
HTTP/1.1规范(第9.6节)讨论了适当的响应/错误代码。但它并不涉及响应内容。
你期望什么?对我来说,简单的HTTP响应代码(如200等)似乎很直接明了。
Create
---------------------------------------------------------------------
Success - 201 Created - Return created object
Failure - 400 Invalid request - Return details about the failure
Async fire and forget operation - 202 Accepted - Optionally return url for polling status
Update
---------------------------------------------------------------------
Success - 200 Ok - Return the updated object
Success - 204 NoContent
Failure - 404 NotFound - The targeted entity identifier does not exist
Failure - 400 Invalid request - Return details about the failure
Async fire and forget operation - 202 Accepted - Optionally return url for polling status
Patch
---------------------------------------------------------------------
Success - 200 Ok - Return the patched object
Success - 204 NoContent
Failure - 404 NotFound - The targeted entity identifier does not exist
Failure - 400 Invalid request - Return details about the failure
Async fire and forget operation - 202 Accepted - Optionally return url for polling status
Delete
---------------------------------------------------------------------
Success - 200 Ok - No content
Success - 200 Ok - When element attempting to be deleted does not exist
Async fire and forget operation - 202 Accepted - Optionally return url for polling status
Get
---------------------------------------------------------------------
Success - 200 Ok - With the list of resulting entities matching the search criteria
Success - 200 Ok - With an empty array
Get specific
---------------------------------------------------------------------
Success - 200 Ok - The entity matching the identifier specified is returned as content
Failure - 404 NotFound - No content
Action
---------------------------------------------------------------------
Success - 200 Ok - Return content where appropriate
Success - 204 NoContent
Failure - 400 - Return details about the failure
Async fire and forget operation - 202 Accepted - Optionally return url for polling status
Generic results
---------------------------------------------------------------------
Authorization error 401 Unauthorized
Authentication error 403 Forbidden
For methods not supported 405
Generic server error 500
返回HTTP 201响应代码,表示“已创建”,并附带一个“位置”头,指向客户端可以找到新创建的资源的位置。
PUT
用于更新资源而不是创建或获取。无状态资源
和有状态资源
:
无状态资源 对于这些资源,只需返回带空内容的HttpCode即可。
有状态资源 例如:资源的版本。对于此类资源,您必须在要更改它时提供版本,因此返回完整的资源或将版本返回给客户端,这样在更新操作之后,客户端不需要发送一个get请求。
RFC7231没有明确规定PUT方法的响应体。你可以按照自己的方式处理。例如:
HTTP并没有准确定义PUT方法对原始服务器状态的影响,除了用户代理请求的意图和原始服务器响应的语义之外。
如果目标资源没有当前表示,并且PUT成功创建了一个表示,则原始服务器必须通过发送201(Created)响应来通知用户代理。
对PUT方法的响应不能被缓存。
200
作为PUT,DELETE或任何其他方法的规定(特别是10.2成功的2xx和10.2.1 200 OK节)。我有什么遗漏吗?比如Mozilla成为W3和IETF的老板? ;) 或者他们可能从未听说过Postel的鲁棒性原则。 - system PAUSE