如何在 REST 中支持部分更新 (PATCH) [英] How to support Partial Updates (PATCH) in REST

查看:50
本文介绍了如何在 REST 中支持部分更新 (PATCH)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的资源实现部分更新,因为我有大量资源并想从中更新部分信息.我已经浏览了以下链接但没有
能够判断是使用 HTTP POST 还是 PATCH 方法.

I want to implement the partial updates for my resource as i have large resource and want to update the partial information from it.I have gone through the following links but not
able to figure out whether to use HTTP POST or PATCH methods.

REST 的 HTTP 修改动词?

如何提交 RESTful 部分更新?

http://jacobian.org/writing/rest-worst-practices/

https://github.com/archiloque/rest-client/issues/79

http://tools.ietf.org/html/draft-dusseault-http-patch-16

http://greenbytes.de/tech/webdav/draft-dusseault-http-patch-06.html

http://jasonsirota.com/rest-partial-updates-use-post-put-or-patch

http://bitworking.org/news/296/How-To-Do-RESTful-Partial-Updates

https://github.com/dharmafly/jsonpatch.js

请为此提出任何有效的解决方案.

Please suggest any valid solution for this.

推荐答案

根据 RFC5789 (http://tools.ietf.org/html/rfc5789),这正是 PATCH 的用途:

According to RFC5789 (http://tools.ietf.org/html/rfc5789), this is precisely what PATCH is for:

扩展超文本传输​​协议 (HTTP) 的几个应用程序需要一个特性来做部分资源修改.现有的HTTP PUT 方法只允许完全替换一个文档.该提案添加了一个新的 HTTP 方法 PATCH,以修改现有的HTTP 资源.

Several applications extending the Hypertext Transfer Protocol (HTTP) require a feature to do partial resource modification. The existing HTTP PUT method only allows a complete replacement of a document. This proposal adds a new HTTP method, PATCH, to modify an existing HTTP resource.

PATCH 和 PUT 的区别描述为:

The distinction between PATCH and PUT is described as:

PUT 和 PATCH 请求的区别体现在服务器处理封闭实体以修改资源的方式由 Request-URI 标识.在 PUT 请求中,封闭的实体被认为是存储在资源上的修改版本源服务器,客户端请求存储的版本被替换.但是,使用 PATCH,封闭的实体包含一组描述资源当前如何驻留在应修改原始服务器以生成新版本.

The difference between the PUT and PATCH requests is reflected in the way the server processes the enclosed entity to modify the resource identified by the Request-URI. In a PUT request, the enclosed entity is considered to be a modified version of the resource stored on the origin server, and the client is requesting that the stored version be replaced. With PATCH, however, the enclosed entity contains a set of instructions describing how a resource currently residing on the origin server should be modified to produce a new version.

还描述了 POST 的局限性:

The limitations of POST are also described:

已经定义了 PUT 方法来覆盖资源具有完整的新主体,并且不能重复使用进行部分更改.否则,代理和缓存,甚至客户端和服务器,可能会对手术结果一头雾水.POST 已经被使用,但是没有广泛的互操作性(一方面,没有标准的方法来发现补丁格式支持)[...]

The PUT method is already defined to overwrite a resource with a complete new body, and cannot be reused to do partial changes. Otherwise, proxies and caches, and even clients and servers, may get confused as to the result of the operation. POST is already used but without broad interoperability (for one, there is no standard way to discover patch format support) [...]

我建议您阅读 RFC 并做出自己的决定,但对我来说这似乎相当明确 - PATCH 请求应作为部分更新进行处理.(注意,它们不是幂等的,与 PUT 不同.)

I would suggest you read the RFC and make up your own mind, but to me this seems fairly clear-cut - PATCH requests should be processed as partial updates. (NB they are NOT idempotent, unlike PUT.)

正如尤金在评论中指出的那样,尽管 PATCH 请求既不安全也不幂等,如 [RFC2616] 所定义",它们可以这样做:

as pointed out by Eugene in the comments, although PATCH requests are "neither safe nor idempotent as defined by [RFC2616]", they can be made so:

PATCH 请求可以以幂等的方式发出,这也有助于防止两个人之间的冲突导致不良结果在相似的时间范围内对同一资源的 PATCH 请求.来自多个 PATCH 请求的冲突可能比PUT 冲突,因为某些补丁格式需要从已知基点,否则它们将破坏资源.客户使用这种补丁应用程序应该使用条件请求如果资源已更新,则请求将失败自客户端上次访问资源以来.例如,客户端可以在 PATCH 的 If-Match 标头中使用强 ETag [RFC2616]请求.

A PATCH request can be issued in such a way as to be idempotent, which also helps prevent bad outcomes from collisions between two PATCH requests on the same resource in a similar time frame. Collisions from multiple PATCH requests may be more dangerous than PUT collisions because some patch formats need to operate from a known base-point or else they will corrupt the resource. Clients using this kind of patch application SHOULD use a conditional request such that the request will fail if the resource has been updated since the client last accessed the resource. For example, the client can use a strong ETag [RFC2616] in an If-Match header on the PATCH request.

这篇关于如何在 REST 中支持部分更新 (PATCH)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆