Translate

Friday 10 November 2023

What’s the exact difference between put & patch http methods in Mule Soft

 

What’s the exact difference between put & patch http methods in Mule Soft?

The main difference between the HTTP PUT and PATCH methods is that the PUT method replaces the entire resource, while the PATCH method updates only the specified parts of the resource.

https://youtu.be/SvrPd0_kI2s

PUT

The PUT method is used to replace the entire resource with the data that is sent in the request body. The PUT method is idempotent, which means that it can be executed multiple times without changing the outcome.

PATCH

The PATCH method is used to update only the specified parts of the resource. The PATCH method is not idempotent, which means that the outcome of the request may change depending on how many times it is executed.

 

The following table summarizes the key differences between the HTTP PUT and PATCH methods:

Property

PUT

PATCH

Replaces the entire resource

Yes

No

Updates only the specified parts of the resource

No

Yes

Idempotent

Yes

No

Examples

The following example shows how to use the PUT method to replace the entire resource:

 

PUT /api/products/1 HTTP/1.1
Host: example.com
Content-Type: application/json

{
  "name": "My Product",
  "description": "This is my product."
}

 

The following example shows how to use the PATCH method to update the specified parts of the resource:

 

PATCH /api/products/1 HTTP/1.1
Host: example.com
Content-Type: application/json

{
  "name": "My Updated Product"
}


In the second example, the PATCH method will update the name property of the product with the value My Updated Product. The other properties of the product will remain unchanged.

Which method to use?

You should use the PUT method if you need to replace the entire resource. You should use the PATCH method if you need to update only the specified parts of the resource.

In general, it is recommended to use the PATCH method whenever possible, as it is more efficient and less resource-intensive than the PUT method.

 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.