http-patch – 根据HTTP PATCH RFC,文档的部分表示是有效的“一组更改”吗?

这是
RFC 5789所说的:

The PATCH method requests that a set of changes described in the request entity be applied to the resource identified by the Request-URI. The set of changes is represented in a format called a “patch document” identified by a media type. If the Request-URI does not point to an existing resource, the server MAY create a new resource, depending on the patch document type (whether it can logically modify a null resource) and permissions, etc.

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.

假设我有{“login”:“x”,“enabled”:true},我想禁用它.

根据“Please. Don’t Patch Like An Idiot.”之后,适当的PATCH请求将是

[{ "op": "replace", "path": "/enabled", "value": false }]

但是,让我们来看看这个请求:

{ "enabled": false }

它还包含一组描述如何修改当前驻留在源服务器上的资源的指令,唯一的区别是使用了JSON属性而不是JSON对象.

它似乎不那么强大,但是如果需要,数组更改可以有一些其他特殊语法(例如{“a”:{“add”:[],“remove”:[]}}),并且服务器逻辑可能无法处理反正更强大的东西.

根据RFC,这是一个不正确的PATCH请求吗?如果是这样,为什么?
而另一方面,{“op”:“disable”}是否是正确的PATCH请求?

最佳答案

the only difference is that JSON property is used instead of JSON object.

它实际上比那更深一些.对RFC 6902的引用很重要.第一个请求有一个Content-Type of application / json-patch json,但第二个请求是application / json

重要的是你使用’差异媒体类型’,这是一个有用的目的.你不必使用JSON-PATCH,(我是json-merge-patch的忠实粉丝),但你不能只使用你想要的任何东西.您在第二部分中询问的内容基本上是“我可以制作自己的媒体类型”,答案是“是”,请将其记录下来并在IANA注册.

点赞