我想知道人们对在响应体中不返回任何(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)
其他回答
HTTP响应的头和正文之间是有区别的。PUT永远不应该返回正文,但必须在报头中返回响应代码。如果成功就选择200,如果不成功就选择4xx。没有空返回码这样的东西。你为什么想做这个?
空的Request体符合GET请求的原始目的,空的响应体也符合PUT请求的原始目的。
Http方法“PUT”可能具有不同的Http状态,根据传递的请求- uri的执行状态。下表可能有助于理解-
理想情况下,它将返回一个成功/失败响应。
如果REST API的后端是一个SQL关系数据库,那么
您应该在每个可以更新的记录中都有RowVersion(以避免丢失更新问题) 您应该总是在PUT之后返回记录的新副本(以获得新的RowVersion)。
如果您不关心丢失的更新,或者您希望强制客户端在PUT之后立即执行GET,那么不要从PUT返回任何东西。
推荐文章
- 哪个HTTP状态代码表示“尚未准备好,稍后再试”?
- Django REST框架:向ModelSerializer添加额外字段
- Spring引导应用程序作为服务
- 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如何自定义错误处理?