HTTP Patch Request

Unlike PUT Request, PATCH does partial update e.g. Fields that need to be updated by the client, only that field is updated without modifying the other field.

So in the previous example, we have to send only the name and email field in the request body.

{
"first_name":"Geeky", // field that to be updated
"email":"hello@geeky.com", // field that to be updated
}

Example: The following code demonstrates the PATCH request method

Javascript
let PatchRequest = () => {
  // sending PUT request with fetch API in javascript
  fetch("https://reqres.in/api/users/2", {
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json"
    },
    method: "PATCH",     

    // Fields that to be updated are passed
    body: JSON.stringify({
      email: "hello@geeky.com",
      first_name: "Geeky"
    })
  })
    .then(function (response) {

      // console.log(response);
      return response.json();
    })
    .then(function (data) {
      console.log(data);
    });
};

PatchRequest();

Output:

patch request

In the above example, we have made a PATCH request to the server, with a payload attached to the body. If we want to update the email and first_name, with a PATCH request then we have to send only the fields that have to be updated e.g first_name and email.

Difference Between PUT and PATCH Request

When working with APIs, figuring out the right way to update resources can be tricky. Both PUT and PATCH requests are used for this purpose, but they have distinct functionalities. This guide will break down the key differences between these two methods, helping you choose the most appropriate approach for your needs.

Similar Reads

HTTP PUT Request

PUT is a method of modifying resources where the client sends data that updates the entire resource. PUT is similar to POST in that it can create resources, but it does so when there is a defined URL wherein PUT replaces the entire resource if it exists or creates new if it does not exist....

HTTP Patch Request:

Unlike PUT Request, PATCH does partial update e.g. Fields that need to be updated by the client, only that field is updated without modifying the other field....

Difference between PUT and PATCH Requests:

PUT                                           PATCH      PUT is a method of modifying resource where the client sends data that updates the entire resource .                                     PATCH is a method of modifying resources where the client sends partial data that is to be updated without modifying the entire data.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 replacedWith 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.HTTP PUT is said to be idempotent, So if you send retry a request multiple times, that should be equivalent to a single request modificationHTTP PATCH is considered idempotent. When a PATCH request is sent multiple times, it will have the same effect as sending it onceIt has High Bandwidth Since Only data that need to be modified if send in the request body as a payload , It has Low Bandwidth...

Contact Us