Differences between PUT and PATCH Request

Jasmine Grover logo

Jasmine Grover

Content Strategy Manager

Thе PUT request is usеd to updatе a resource or crеatе a nеw resource if it doеsn't еxist.

It replaces thе entire еxisting resource with thе nеw representation providеd in thе request.

While PATCH merely updates the modifications that are requested, PUT replaces the entire resource. PATCH is more effective for partial updates since PUT calls for delivering the complete resource, whereas PATCH merely transmits the changes.

  • While PATCH is used to make partial changes or alterations to the resource, PUT is used to replace the resource entirely.
  • PATCH simply involves delivering particular modifications to the resource, whereas PUT also requires providing the whole representation of the resource.
  • PUT requests are idempotent, therefore making more than one will have the same result as making just one. 
  • PATCH requests may be created to be idempotent if they are.

Key terms: PUT, PATCH, Request, HTTP, Server, Web, Resource, Caching, Action, Server


Difference between PUT and PATCH Request

[Click Here for Sample Questions]

The major differences between PUT and PATCH Request are follows:

Aspect PUT PATCH
Purpose Replaces the entire resource. Partially updates the resource.
Request Body Requires sending the complete resource. Requires sending only the changes.
Idempotence Inherently idempotent. Can be idempotent if designed so.
Efficiency Less efficient as it sends the entire resource. More data-efficient as it sends only what's changed.
Safety Considered less safe as it can lead to data loss by overwriting the resource. Generally safer, as it avoids overwriting the entire resource.
Resource State Requires the client to have the complete resource state. Adaptable when the client may not have the complete resource state.
Use Cases Used when you have the complete resource and want to replace it entirely. Preferred for making minor updates, like modifying a specific field in a database record.
Network Traffic Generates more network traffic due to the complete resource transfer. Generates less network traffic, reducing data transmission.
Performance May be less efficient in terms of performance and speed due to resource replacement. Can be more efficient and faster for minor updates.
Caching Less cache-friendly, as it replaces the resource each time. More cache-friendly, as it only modifies the required parts.
Error Handling Errors may require rolling back the entire resource, potentially complex. Easier to handle errors and recover from issues, as only specific changes need correction.

Read More:


What is a PUT Request?

[Click Here for Sample Questions]

An essential HTTP (Hypertext Transfer Protocol) technique called a PUT request is used to replace or update an existing resource on a web server. 

  • This technique, which enables clients to transmit a new representation of the resource to the server instead of the previous one, is one of the main ways to alter resources.
  • The same request may be sent more than once and yet have the same result since PUT requests are idempotent. 
  • This is so that consistency in resource state may be guaranteed by PUT.

Here is an illustration of a PUT request in action

Imagine having a straightforward online application that keeps track of a list of books. On the server, a JSON object with characteristics is used to represent each book.

First State

Suppose you have a book resource on your server that looks like this:

{

"title": "The Great Gatsby",

"author": "F. Scott Fitzgerald",

"isbn": "978-0743273565"

}

Client Request

Now, a client wants to update the author's name from "F. Scott Fitzgerald" to "Francis Scott Fitzgerald." To do this, the client sends a PUT request to the server with the updated representation of the book:

PUT /books/1 HTTP/1.1

Host: example.com

Content-Type: application/json

{

"title": "The Great Gatsby",

"author": "Francis Scott Fitzgerald",

"isbn": "978-0743273565"

}

The client delivers the modified book representation in JSON format along with the resource URL, which is /books/1, in this request.

Processing by the server

When the server gets this PUT request, it recognizes that the client is requesting to replace the current resource at the URL /books/1 with the new representation specified in the request body. The server executes the request and updates the values in the book's data.

Response

The server provides a status code in response that describes how the operation went. The server normally replies with a 200 OK status code if the update is successful.

HTTP/1.1 200 OK

Content-Type: application/json

{

"message": "Book updated successfully."

}

Now that their request has been changed, the client may be sure that the resource at /books/1 has been done so.


What is a PATCH Request?

[Click Here for Sample Questions]

An HTTP (Hypertext Transfer Protocol) technique called a PATCH request is used to only partly update a resource on a web server. 

  • A PATCH request merely modifies particular portions of the resource, as opposed to a PUT request, which alters the resource's complete representation. 
  • PATCH requests can be more effective in some situations since they are frequently used to change individual fields or properties.

Here's an illustration of a PATCH request in action

Think about a web application you have that handles user profiles. Each user profile is stored on the server as a JSON object that includes the user's name, email address, and profile image, among other data.

First State

Assume for the moment that your server has a user profile with the following representation:

"id": 123, 

"name": "John Doe", 

"email": "john.doe@example.com", 

"profilePicture": "avatar.jpg" 

}

At present, a customer requests changing the user's email address from "john.doe@example.com" to "john@example.com." To do this, the client issues a PATCH request to the server, specifying the precise modifications to be made:

PATCH /users/123 HTTP/1.1

Host: example.com

Content-Type: application/json-patch+json

[

{ "op": "replace", "path": "/email", "value": "john@example.com" }

]

The client submits this request with the application/json-patch+json content type, the JSON Patch document in the request body, and the resource URL of /users/123. The JSON Patch document includes the action ("replace"), the URL for the attribute that has to be changed ("/email"), and the desired new value ("john@example.com").

Server Processing

The server recognizes that the client is attempting to apply a partial modification to the resource located at the URL /users/123 when it gets this PATCH request. The request is processed by the server, which then finds the "/email" property that was requested and modifies it with the new value from the JSON Patch document.

Response

The server responds with the proper status code to let you know how the operation went. It normally responds with a 200 OK response code if the update is successful.

HTTP/1.1 200 OK

Content-Type: application/json

{

"message": "User profile updated successfully."

}

The user's email address has now been adjusted as required, and the customer has been notified.


Things to Remember

  • PUT is used to completely replace a resource, whereas PATCH is used to modify a resource only partially. 
  • PUT replaces the full resource, whereas PATCH modifies only a portion of it.
  • PATCH just provides the modifications required to update the resource, whereas PUT involves giving the resource's whole representation in the request.
  • PATCH uses less network bandwidth than PUT, which sends the complete resource, making it more data-efficient for performing partial modifications.
  • PUT is frequently seen as dangerous since it replaces the resource, which might result in data loss. 
  • PATCH typically performs adjustments without the danger of overwriting the entire resource, hence it is safer.

Also Read:


Sample Questions

Ques: What distinguishes a PUT request from a PATCH request in particular? (1 mark)

Ans: While PATCH modifies a small portion of the resource without completely replacing it, PUT replaces the entire resource with a new representation.

Ques: Why is a PATCH request preferred over a PUT request in some circumstances? (1 mark)

Ans: When compared to PUT, which sends the whole resource, PATCH is more effective since it simply transmits the modifications required to update a resource.

Ques: Why does it matter how idempotence for PUT and PATCH requests differs? (1 mark)

Ans: PUT requests are naturally idempotent, which means that several requests that are exactly the same have the same result. When processing repeated requests, PATCH requests may be idempotent, ensuring consistency in the resource state.

Ques: What is a PATCH request's main benefit when it comes to resource changes for web applications? (1 mark)

Ans: PATCH makes it possible to change particular fields or resource properties since it supports effective partial updates that minimize data transmission.

Ques: Describe how resource caching is affected by PUT and PATCH requests. (1 mark)

Ans: PUT changes the resource altogether, potentially invalidating caches, and making it less cache-friendly. PATCH allows caches to continue to function while just changing a small portion of the resource, making it more cache-friendly.

Ques: How can PUT and PATCH requests help web applications and APIs manage resource state? (2 marks)

Ans: PATCH is a more flexible option for managing resource states when just particular modifications are required, whereas PUT is appropriate for instances needing entire resource replacement. In order to choose the best request mechanism, it is crucial to comprehend the use case.

Ques: What is the primary purpose of a PUT request? (1 mark)
A) Update a resource completely
B) Update a resource partially
C) Retrieve data
D) Delete a resource

Ans. A) Update a resource completely

Explanation: The PUT request is used to update a resource or create a new resource if it doesn't exist. It typically involves sending the entire updated representation of the resource, replacing the existing resource with the new data.

Ques: What is the primary purpose of a PATCH request? (1 mark)
A) Update a resource completely
B) Update a resource partially
C) Retrieve data
D) Delete a resource

Ans. B) Update a resource partially

Explanation: The PATCH request is used to apply partial modifications to a resource. Unlike PUT, it doesn't require sending the entire updated representation; instead, it includes only the changes that need to be applied.

Ques: Which HTTP status code is commonly associated with a successful PUT request? (1 mark)
A) 200 OK
B) 201 Created
C) 204 No Content
D) 404 Not Found

Ans. A) 200 OK

Explanation: A successful PUT request is often responded to with a 200 OK status code, indicating that the request was successful and the resource has been updated.

Ques: Which HTTP method is idempotent, meaning that making multiple identical requests will produce the same result as a single request? (1 mark)
A) POST
B) PUT
C) PATCH
D) DELETE

Ans. B) PUT 

Explanation: It is an idempotent HTTP method. Idempotency means that making the same request multiple times will have the same effect as making it once. PATCH and DELETE may or may not be idempotent, depending on the specific implementation.

For Latest Updates on Upcoming Board Exams, Click Here: https://t.me/class_10_12_board_updates


Check-Out: 

Comments



No Comments To Show