Difference Between PUT and PATCH Request - GeeksforGeeks

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.
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  fetch("https://reqres.in/api/users/2", {    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  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:
  
arpitjain1616
http
JavaScript-Questions
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Difference between var, let and const keywords in JavaScript
JavaScript | Promises
How to get character array from string in JavaScript?
How to filter object array based on attributes?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript