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


当前回答

Put和Patch方法类似。但是在rails中有不同的方法 如果我们想要更新/替换整个记录,那么我们必须使用Put方法。 如果我们想要更新特定的记录使用补丁方法。

其他回答

Put和Patch方法类似。但是在rails中有不同的方法 如果我们想要更新/替换整个记录,那么我们必须使用Put方法。 如果我们想要更新特定的记录使用补丁方法。

根据HTTP术语,PUT请求就像一个数据库更新语句。 PUT -用于修改现有资源(先前post)。另一方面,PATCH请求用于更新现有资源的某些部分。

例如:

客户详细信息:

// This is just a example.

firstName = "James";
lastName = "Anderson";
email = "email@domain.com";
phoneNumber = "+92 1234567890";
//..

当我们想要更新到整个记录?我们必须使用Http PUT动词。

如:

// Customer Details Updated.

firstName = "James++++";
lastName = "Anderson++++";
email = "email@Updated.com";
phoneNumber = "+92 0987654321";
//..

另一方面,如果我们只想更新记录的一部分而不是整个记录,那么使用Http PATCH谓词。 如:

   // Only Customer firstName and lastName is Updated.

    firstName = "Updated FirstName";
    lastName = "Updated LastName";
   //..

Put和post:

当使用PUT请求时,我们必须发送所有参数,如firstName, lastName, email, phoneNumber,其中在补丁请求中只发送我们想要更新的参数,它不会影响或改变其他数据。

详情请访问:https://fullstack-developer.academy/restful-api-design-post-vs-put-vs-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请求:

{ "first": "Nazmul", "last": "hasan" } 

但是这里有一个使用put请求的问题:当我想发送put请求时,我必须发送所有两个参数,这是第一个和最后一个(而我只需要更新第一个),所以必须再次与put请求一起发送它们。

补丁: 另一方面,补丁请求说:只指定你需要更新的数据,它不会影响或改变其他数据。 所以不需要再次发送所有值。我只需要改名字吗?只需要在补丁请求中指定第一个。

PUT和PATCH方法在本质上是相似的,但有一个关键的区别。

PUT -在PUT请求中,所包含的实体将被认为是驻留在服务器上的资源的修改版本,它将被这个修改的实体所取代。

PATCH -在PATCH请求中,封闭的实体包含一组指令,说明驻留在服务器上的实体将如何被修改以产生一个新的版本。