我想知道人们对在响应体中不返回任何(null)的RESTful PUT操作有什么看法。
当前回答
我同意上面投票最多的答案,但我想更详细地说明这一点。
REST不是一个标准,因此每个人都可以创建和记录自己的API,只要它符合目的,并且得到了良好的文档记录,得到了开发人员和消费者的同意和理解。 真正重要的是url标识了公开的资源。 我已经使用Http Rest api很多年了,我想分享我通常使用的标准方法,不是因为它是完美的,或者“行为规则”,只是因为我发现它很容易使用并向他人解释。
请注意,我没有提到超媒体,因为这远远超出了回答的目的,我宁愿把它放在范围之外,但如果有兴趣,可以在OData规范中找到一份不错的阅读材料。
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响应的头和正文之间是有区别的。PUT永远不应该返回正文,但必须在报头中返回响应代码。如果成功就选择200,如果不成功就选择4xx。没有空返回码这样的东西。你为什么想做这个?
我同意上面投票最多的答案,但我想更详细地说明这一点。
REST不是一个标准,因此每个人都可以创建和记录自己的API,只要它符合目的,并且得到了良好的文档记录,得到了开发人员和消费者的同意和理解。 真正重要的是url标识了公开的资源。 我已经使用Http Rest api很多年了,我想分享我通常使用的标准方法,不是因为它是完美的,或者“行为规则”,只是因为我发现它很容易使用并向他人解释。
请注意,我没有提到超媒体,因为这远远超出了回答的目的,我宁愿把它放在范围之外,但如果有兴趣,可以在OData规范中找到一份不错的阅读材料。
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
与这里的大多数答案相反,我实际上认为PUT应该返回更新后的资源(当然除了HTTP代码之外)。
您希望将资源作为PUT操作的响应返回的原因是,当您向服务器发送资源表示时,服务器也可以对该资源应用一些处理,因此客户端希望知道请求成功完成后该资源的样子。(否则它将不得不发出另一个GET请求)。
“Created”的Http响应代码201以及指向客户端可以找到新创建资源的“Location”标头。
HTTP规范(RFC 2616)有许多适用的建议。以下是我的解读:
HTTP status code 200 OK for a successful PUT of an update to an existing resource. No response body needed. (Per Section 9.6, 204 No Content is even more appropriate.) HTTP status code 201 Created for a successful PUT of a new resource, with the most specific URI for the new resource returned in the Location header field and any other relevant URIs and metadata of the resource echoed in the response body. (RFC 2616 Section 10.2.2) HTTP status code 409 Conflict for a PUT that is unsuccessful due to a 3rd-party modification, with a list of differences between the attempted update and the current resource in the response body. (RFC 2616 Section 10.4.10) HTTP status code 400 Bad Request for an unsuccessful PUT, with natural-language text (such as English) in the response body that explains why the PUT failed. (RFC 2616 Section 10.4)
推荐文章
- 跨REST微服务的事务?
- JSF资源库的用途是什么,应该如何使用它?
- 获取操作系统级别的系统信息
- getResourceAsStream返回null
- PATCH和PUT请求的主要区别是什么?
- 调用webrequest, POST参数
- 如何在Spring RestTemplate请求上设置“接受:”头?
- REST API最佳实践:查询字符串中的参数vs请求体中的参数
- Java中SOAP和rest式web服务的主要区别
- Android Scala编程
- 如何为Java创建REST客户端?
- Android 8.1升级后start前台失败
- file_get_contents(): SSL operation failed with code 1, failed to enable crypto
- 在HttpClient和WebClient之间进行选择
- 执行没有实体主体的HTTP POST被认为是不好的做法吗?