如何在 REST 中支持部分更新 (PATCH)
我想对我的资源实现部分更新,因为我有大量资源,并且想从中更新部分信息。我已经浏览了以下链接,但没有
能够确定是否使用 HTTP POST 或 PATCH 方法。
http://jacobian.org/writing/rest-worst-practices/
https://github.com/archiloque/rest-client/issues/79
https://datatracker.ietf.org/doc/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
请为此提出任何有效的解决方案。
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.
How to submit RESTful partial updates?
http://jacobian.org/writing/rest-worst-practices/
https://github.com/archiloque/rest-client/issues/79
https://datatracker.ietf.org/doc/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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 RFC5789 (https://www.rfc-editor.org/rfc/rfc5789),这正是 PATCH 的用途:
PATCH和PUT的区别描述为:
还描述了 POST 的局限性:
我建议您阅读 RFC 并做出自己的决定,但对我来说,这似乎相当明确 - PATCH 请求应作为部分更新进行处理。 (注意,它们不是幂等的,与 PUT 不同。)
编辑:正如 Eugene 在评论中指出的那样,尽管 PATCH 请求是
“既不安全也不幂等,如 [RFC2616] 定义的那样”
,但它们可以所以:According to RFC5789 (https://www.rfc-editor.org/rfc/rfc5789), this is precisely what PATCH is for:
The distinction between PATCH and PUT is described as:
The limitations of POST are also described:
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.)
EDIT: 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:您应该使用 RFC-7386“json merge PATCH”中描述的 PATCH 方法。
例如,如果您想更改“a”的值并删除资源中的“f”,例如:
您可以通过发送以下内容来实现:
You should use method PATCH like described in RFC-7386 "json merge PATCH".
E.g. if you want to change value of "a" and removing "f" in resource like:
You can achive this by sending:
PATCH 与补丁格式一起使用,仅用于文档级补丁(也称为实际表示的差异)。其用于其他目的是可疑和有争议的,并且尚不清楚该方法是否是为非媒体类型用途而设计的。
一般来说,POST 是正确的方法,但您可能希望将资源拆分为多个资源并修改它们。
[为清楚起见进行编辑,因为有些人不阅读评论]
PATCH is to be used with a patch format, for document-level patching only (aka a diff on the actual representation). Its use for other purposes is dubious and debatable, and it's not clear that the method was designed for non-media-type uses.
In general a POST will be the right approach, but you may want to split your resource into multiple resources instead and modify those instead.
[Edited for clarity, as some don't read comments]