Skip to content
Related Articles

Related Articles

Improve Article

Difference Between PUT and PATCH Request

  • Difficulty Level : Easy
  • Last Updated : 13 Sep, 2021

In this article, we will talk about the difference between PUT and PATCH HTTP requests. PUT and PATCH requests are HTTP verbs and both relate to updating the resources at a location.

PUT HTTP 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.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

For example, When you want to update the Candidate name and email, you have to send all the parameters of the Candidate including those not to be updated in the request body, otherwise, it will simply replace the entire resource with the name and email.

{
  id: 8,
  email: "lindsay.ferguson@reqres.in",

  // field to be updated
  first_name: "Lindsay",

  // field to be updated
  last_name: "Ferguson",
  avatar: "https://reqres.in/img/faces/8-image.jpg"
}

let us take an example to understand PUT requests by sending only the fields that we want to update. 



Example: The following code demonstrates the PUT request method 

Javascript




let PutRequest = () => {
  // Sending PUT request with fetch API in javascript
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json"
    },
    method: "PUT",
 
    // Sending only the fields that to be updated
    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);
    });
};
 
PutRequest();

Output:

example of put request

In the above example, we have made a PUT request to the server, with a payload attached to the body. If we want to update the name and email, with a PUT request we have to send all the other fields such id,avatarlast_name, to the server, otherwise, it replaces the data with the payload passed as we can see in the above example.

PATCH HTTP 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
    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:

                                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 modification HTTP PATCH is basically said to be non-idempotent. So if you retry the request N times, you will end up having N resources with N different URIs created on the server.
It has High Bandwidth Since Only data that need to be modified if send in the request body as a payload , It has Low Bandwidth 

  




My Personal Notes arrow_drop_up
Recommended Articles
Page :