我在Rails应用程序中使用PUT请求。现在,浏览器实现了一个新的HTTP动词PATCH。所以,我想知道PATCH和PUT请求之间的主要区别是什么,以及我们什么时候应该使用其中一个或另一个。
当前回答
Differences between PUT and PATCH The main difference between PUT and PATCH requests is witnessed in the way the server processes the enclosed entity to update the resource identified by the Request-URI. When making a PUT request, the enclosed entity is viewed as the modified version of the resource saved on the original server, and the client is requesting to replace it. However, with PATCH, the enclosed entity boasts a set of instructions that describe how a resource stored on the original server should be partially modified to create a new version.
第二个区别是当涉及到等幂的时候。HTTP PUT被称为幂等的,因为它总是在每次发出几个请求后产生相同的结果。另一方面,HTTP PATCH基本上被认为是非幂等的。然而,根据执行的位置,可以使它成为幂等的。
其他回答
PUT和PATCH方法在本质上是相似的,但有一个关键的区别。
PUT -在PUT请求中,所包含的实体将被认为是驻留在服务器上的资源的修改版本,它将被这个修改的实体所取代。
PATCH -在PATCH请求中,封闭的实体包含一组指令,说明驻留在服务器上的实体将如何被修改以产生一个新的版本。
Differences between PUT and PATCH The main difference between PUT and PATCH requests is witnessed in the way the server processes the enclosed entity to update the resource identified by the Request-URI. When making a PUT request, the enclosed entity is viewed as the modified version of the resource saved on the original server, and the client is requesting to replace it. However, with PATCH, the enclosed entity boasts a set of instructions that describe how a resource stored on the original server should be partially modified to create a new version.
第二个区别是当涉及到等幂的时候。HTTP PUT被称为幂等的,因为它总是在每次发出几个请求后产生相同的结果。另一方面,HTTP PATCH基本上被认为是非幂等的。然而,根据执行的位置,可以使它成为幂等的。
在进行更新时,PUT over PATCH有一些限制。使用PUT要求我们指定所有属性,即使我们只想更改一个属性。 但是如果我们使用PATCH方法,我们可以只更新我们需要的字段,而不需要提及所有的字段。PATCH不允许修改数组中的值,也不允许删除属性或数组项。
Put和Patch方法类似。但是在rails中有不同的方法 如果我们想要更新/替换整个记录,那么我们必须使用Put方法。 如果我们想要更新特定的记录使用补丁方法。
把: 如果我想更新我的名字,那么我发送一个put请求:
{ "first": "Nazmul", "last": "hasan" }
但是这里有一个使用put请求的问题:当我想发送put请求时,我必须发送所有两个参数,这是第一个和最后一个(而我只需要更新第一个),所以必须再次与put请求一起发送它们。
补丁: 另一方面,补丁请求说:只指定你需要更新的数据,它不会影响或改变其他数据。 所以不需要再次发送所有值。我只需要改名字吗?只需要在补丁请求中指定第一个。
推荐文章
- 什么是HTTP“主机”报头?
- 哪个HTTP状态代码表示“尚未准备好,稍后再试”?
- 如何阻止恶意代码欺骗“Origin”报头来利用CORS?
- 为什么说“HTTP是无状态协议”?
- 我需要HTTP GET请求的内容类型报头吗?
- 我如何简单地从我最新的git提交创建一个补丁?
- 如何让Chrome允许混合内容?
- 正确的方式删除cookies服务器端
- REST DELETE真的是幂等的吗?
- 了解Chrome网络日志“停滞”状态
- 用户代理字符串可以有多大?
- 什么是接受* HTTP报头q=0.5 ?
- HTTP状态码200(缓存)和状态码304之间有什么区别?
- HTTP POST返回错误:417“期望失败。”
- 什么是HTTP中的“406-不可接受的响应”?