我想知道人们对在响应体中不返回任何(null)的RESTful PUT操作有什么看法。
当前回答
我认为服务器可以返回内容以响应PUT。如果您正在使用允许侧载数据的响应信封格式(例如ember-data所使用的格式),那么您还可以包括其他可能通过数据库触发器修改过的对象,等等(侧载数据显式地减少了请求的数量,这似乎是一个优化的好地方)。
如果我只是接受PUT,没有任何报告,我使用状态代码204,没有正文。如果我有什么要报告的,我会使用状态代码200,并包含一个主体。
其他回答
Http方法“PUT”可能具有不同的Http状态,根据传递的请求- uri的执行状态。下表可能有助于理解-
如果REST API的后端是一个SQL关系数据库,那么
您应该在每个可以更新的记录中都有RowVersion(以避免丢失更新问题) 您应该总是在PUT之后返回记录的新副本(以获得新的RowVersion)。
如果您不关心丢失的更新,或者您希望强制客户端在PUT之后立即执行GET,那么不要从PUT返回任何东西。
HTTP/1.1规范(第9.6节)讨论了适当的响应/错误代码。但是,它不处理响应内容。
你想要什么?一个简单的HTTP响应代码(200等)对我来说似乎是直接而明确的。
我同意上面投票最多的答案,但我想更详细地说明这一点。
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
看起来好…虽然我认为一个基本的指示成功/失败/时间张贴/#字节接收/等等。会更好。
编辑:我一直在考虑数据完整性和/或记录保存;元数据,如MD5散列或接收时间的时间戳,可能对大型数据文件有帮助。
推荐文章
- 有没有REST api的命名规范指南?
- 什么是HTTP中的“406-不可接受的响应”?
- 哪些HTTP方法与哪些CRUD方法相匹配?
- RESTful服务中部分更新的最佳实践
- JAX-RS / Jersey如何自定义错误处理?
- 有没有办法在python中做HTTP PUT
- 如何POST表单数据与Spring RestTemplate?
- Restful API服务
- 在哪里放置和如何在基于servlet的应用程序读取配置资源文件?
- 在用nodejs和express创建的REST API中设置响应状态和JSON内容的正确方法
- 如何POST JSON数据与PHP卷曲?
- REST身份验证方案的安全性
- 如何在package.json中使用“main”参数?
- 跨REST微服务的事务?
- JSF资源库的用途是什么,应该如何使用它?