PATCH 和 PUT 请求之间的主要区别是什么? [英] What is the main difference between PATCH and PUT request?

查看:49
本文介绍了PATCH 和 PUT 请求之间的主要区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rails 应用程序中使用了 PUT 请求.现在,浏览器已经实现了一个新的 HTTP 动词 PATCH.所以,我想知道 PATCHPUT 请求之间的主要区别是什么,以及我们什么时候应该使用其中一个.

I am using a PUT request in my Rails application. Now, a new HTTP verb, PATCH has been implemented by browsers. So, I want to know what the main difference between PATCH and PUT requests are, and when we should use one or the other.

推荐答案

HTTP 动词可能是 HTTP 协议中最神秘的东西之一.它们存在,而且数量众多,但它们为什么存在?

HTTP verbs are probably one of the most cryptic things about the HTTP protocol. They exist, and there are many of them, but why do they exist?

Rails 似乎想要支持很多动词,并添加一些 Web 浏览器本身不支持的动词.

Rails seems to want to support many verbs and add some verbs that aren't supported by web browsers natively.

这是一个详尽的 http 动词列表:http://annevankesteren.nl/2007/10/http-methods

Here's an exhaustive list of http verbs: http://annevankesteren.nl/2007/10/http-methods

官方 RFC 中的 HTTP 补丁:https://datatracker.ietf.org/doc/rfc5789/?include_text=1

There the HTTP patch from the official RFC: https://datatracker.ietf.org/doc/rfc5789/?include_text=1

PATCH 方法请求在请求实体被应用到请求所标识的资源上——URI.更改集以称为补丁"的格式表示文档"由媒体类型标识.如果请求 URI 没有指向现有资源,服务器可以创建一个新资源,取决于补丁文件类型(是否可以在逻辑上修改一个空资源)和权限等

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.

PUTPATCH 请求之间的区别体现在服务器处理封闭实体以修改资源的方式由 Request-URI 标识.在 PUT 请求中,包含的实体被认为是存储在资源上的修改版本源服务器,客户端请求存储的版本取代.PATCH,然而,封闭的实体包含一组描述资源当前如何驻留在应修改原始服务器以生成新版本.PATCH方法影响由 Request-URI 标识的资源,并且它也可能对其他资源有副作用;即,新资源可能是通过应用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. The PATCH method affects the resource identified by the Request-URI, and it also MAY have side effects on other resources; i.e., new resources may be created, or existing ones modified, by the application of a PATCH.

据我所知,PATCH 动词不像在 Rails 应用程序中那样使用......据我了解,RFC 补丁动词应该用于发送补丁指令,例如在两个文件之间做一个差异.您无需再次发送整个实体,而是发送一个比重新发送整个实体小得多的补丁.

As far as I know, the PATCH verb is not used as it is in rails applications... As I understand this, the RFC patch verb should be used to send patch instructions like when you do a diff between two files. Instead of sending the whole entity again, you send a patch that could be much smaller than resending the whole entity.

假设您要编辑一个大文件.您编辑 3 行.您只需发送差异,而不是将文件发回.从好的方面来说,发送补丁请求可用于异步合并文件.版本控制系统可能会使用 PATCH 动词远程更新代码.

Imagine you want to edit a huge file. You edit 3 lines. Instead of sending the file back, you just have to send the diff. On the plus side, sending a patch request could be used to merge files asynchronously. A version control system could potentially use the PATCH verb to update code remotely.

另一个可能的用例与 NoSQL 数据库有些相关,它可以存储文档.假设我们使用 JSON 结构从服务器到客户端来回发送数据.如果我们想删除一个字段,我们可以使用类似于 mongodb 中的语法 for $unset.其实mongodb中用来更新文档的方法大概可以用来处理json补丁.

One other possible use case is somewhat related to NoSQL databases, it is possible to store documents. Let say we use a JSON structure to send back and forth data from the server to the client. If we wanted to delete a field, we could use a syntax similar to the one in mongodb for $unset. Actually, the method used in mongodb to update documents could be probably used to handle json patches.

以这个例子为例:

db.products.update(
   { sku: "unknown" },
   { $unset: { quantity: "", instock: "" } }
)

我们可以有这样的东西:

We could have something like this:

PATCH /products?sku=unknown
{ "$unset": { "quantity": "", "instock": "" } }

最后但并非最不重要的一点是,人们可以对 HTTP 动词说他们想说的任何话.只有一个真相,真相在 RFC 中.

Last, but not least, people can say whatever they want about HTTP verbs. There is only one truth, and the truth is in the RFCs.

这篇关于PATCH 和 PUT 请求之间的主要区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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