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

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

问题描述

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

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 方法请求将
请求实体中描述的一组更改应用于Request-
URI标识的资源。这组更改以媒体类型标识的称为patch
document的格式表示。如果Request-URI不是
指向现有资源,则服务器 MAY 创建一个新资源,
,具体取决于补丁文档类型(是否可以在逻辑上修改$ b) $ ba null资源)和权限等。

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.

PUT PATCH 请求之间的差异反映在
方式服务器处理所包含的实体以修改Request-URI标识的资源
。在 PUT 请求中,附带的实体
被视为存储在
源服务器上的资源的修改版本,客户端请求存储的版本为
已被替换。 但是,使用 PATCH ,随附的实体包含一组
指令,这些指令描述了如何修改当前驻留在
源服务器上的资源以生成新版本。
PATCH
方法会影响 Request-URI 标识的资源,还会影响
MAY 对其他资源有副作用;即,通过 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中的语法来 $ 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: "" } }
)

我们可以这样:

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天全站免登陆