我想知道人们对在响应体中不返回任何(null)的RESTful PUT操作有什么看法。
当前回答
理想情况下,它将返回一个成功/失败响应。
其他回答
看起来好…虽然我认为一个基本的指示成功/失败/时间张贴/#字节接收/等等。会更好。
编辑:我一直在考虑数据完整性和/或记录保存;元数据,如MD5散列或接收时间的时间戳,可能对大型数据文件有帮助。
HTTP/1.1规范(第9.6节)讨论了适当的响应/错误代码。但是,它不处理响应内容。
你想要什么?一个简单的HTTP响应代码(200等)对我来说似乎是直接而明确的。
HTTP响应的头和正文之间是有区别的。PUT永远不应该返回正文,但必须在报头中返回响应代码。如果成功就选择200,如果不成功就选择4xx。没有空返回码这样的东西。你为什么想做这个?
我认为服务器可以返回内容以响应PUT。如果您正在使用允许侧载数据的响应信封格式(例如ember-data所使用的格式),那么您还可以包括其他可能通过数据库触发器修改过的对象,等等(侧载数据显式地减少了请求的数量,这似乎是一个优化的好地方)。
如果我只是接受PUT,没有任何报告,我使用状态代码204,没有正文。如果我有什么要报告的,我会使用状态代码200,并包含一个主体。
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和RPC之间的Web服务差异
- REST DELETE真的是幂等的吗?
- 如何用node.js实现一个安全的REST API
- 如何在Node.js内进行远程REST调用?旋度吗?
- Rails new vs create
- 如何在。net中创建和使用资源
- 如何使用邮差导出具体要求文件?
- 有没有REST api的命名规范指南?
- 什么是HTTP中的“406-不可接受的响应”?
- 哪些HTTP方法与哪些CRUD方法相匹配?
- RESTful服务中部分更新的最佳实践
- JAX-RS / Jersey如何自定义错误处理?
- 有没有办法在python中做HTTP PUT
- 如何POST表单数据与Spring RestTemplate?
- Restful API服务