我想知道人们对在响应体中不返回任何(null)的RESTful PUT操作有什么看法。
当前回答
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)
其他回答
“Created”的Http响应代码201以及指向客户端可以找到新创建资源的“Location”标头。
与这里的大多数答案相反,我实际上认为PUT应该返回更新后的资源(当然除了HTTP代码之外)。
您希望将资源作为PUT操作的响应返回的原因是,当您向服务器发送资源表示时,服务器也可以对该资源应用一些处理,因此客户端希望知道请求成功完成后该资源的样子。(否则它将不得不发出另一个GET请求)。
理想情况下,它将返回一个成功/失败响应。
空的Request体符合GET请求的原始目的,空的响应体也符合PUT请求的原始目的。
我认为服务器可以返回内容以响应PUT。如果您正在使用允许侧载数据的响应信封格式(例如ember-data所使用的格式),那么您还可以包括其他可能通过数据库触发器修改过的对象,等等(侧载数据显式地减少了请求的数量,这似乎是一个优化的好地方)。
如果我只是接受PUT,没有任何报告,我使用状态代码204,没有正文。如果我有什么要报告的,我会使用状态代码200,并包含一个主体。
推荐文章
- TextView粗体通过XML文件?
- ReferenceError: description没有定义NodeJs
- REST API - dto还是不是?
- OData和REST web服务的区别
- 在不活动的地方调用getLayoutInflater()
- 用Spring我可以做一个可选的路径变量吗?
- 哪个HTTP状态代码表示“尚未准备好,稍后再试”?
- Django REST框架:向ModelSerializer添加额外字段
- Spring引导应用程序作为服务
- REST和RPC之间的Web服务差异
- REST DELETE真的是幂等的吗?
- 如何用node.js实现一个安全的REST API
- 如何在Node.js内进行远程REST调用?旋度吗?
- Rails new vs create
- 如何在。net中创建和使用资源