Difference between Put and Patch Request


HTTP is an application layer protocol created to transport data between networked devices. In HTTP, a number of methods are accessible. These methods represent the CRUD (create, read, update, delete) operations, in that order. Some HTTP methods share a lot of the same traits. Therefore, before implementing these comparable HTTP methods in an HTTP system, we should thoroughly study them. For instance, there are similarities between the features of the PUT and PATCH methods that may cause confusion. Both are used to update a resource at a particular location. The following are some of the differences between them.

What is PUT Request?

PUT is a technique for updating resources in which the client sends data that updates the whole resource. When creating an entity or resource on an HTTP server, clients use the PUT method. This setup procedure itself can take one of two shapes:

Updating Resource

  • When an entity is requested that doesn't already exist, the server creates it and returns a success code 201 to the client.

  • The server updates the entity, if already exists, and returns a success code of 200 or 204 to the client. Additionally, the server should provide the client with the appropriate error code, often a 4xx or 5xx, if a PUT request encounters a problem. It is used to completely set an entity's information.

In the PUT method, we need to send the whole resource along with the parameters that need to get updated.

Example

If you want to update the phone number and email of a person, you need to send all the parameters along with the phone number and email in order to update a resource. Otherwise the whole resource will be replaced by the phone number and email.

Syntax

PUT /html file HTTP/1.1

Example

PUT/html file HTTP/1.1
Host: example.com
Content- type: text/html
Content- length: 20
<p> New File </p>

Response

The origin server shall send a 201 (Created) response to the user agent if the target resource does not currently have a representation and the PUT request successfully creates one.

HTTP/1.1 201 Created
Content-Location: /new.html

The origin server must send a 200 (OK) or a 204 (No Content) response to indicate that the request was successful if the target resource does already have a current representation and that representation is successfully changed in accordance with the state of the enclosed representation.

HTTP/1.1 204 No Content
Content-Location: /existing.html

What is PATCH Request?

The PATCH technique modifies resource elements only partially. The requested modifications are executed atomically by the PATCH technique. It implies that the server won't modify the target entity if it can't accommodate all of the requested modifications.

In this approach, if the request is carried out correctly, the server sends the client the success code 204. Otherwise, an error code is returned by the server. Fields that the client needs to update only get updated, leaving the remaining fields untouched.

Syntax

PATCH /file.txt HTTP/1.1

Example

PATCH /file.txt HTTP/1.1
Host: www.example.com
Content-Type: application/example
If-Match: "e0023aa4e"
Content-Length: 100

A 2xx status code indicates a successful response.

As the response does not contain a payload body, a 204 response code is utilized in the example below. A payload body might have been present in a 200 code response.

Response

HTTP/1.1 204 No Content
Content-Location: /file.txt
ETag: "e0023aa4f"

Difference between PUT and PATCH Requests

The following table highlights the major differences between PUT and PATCH requests:

PUT

PATCH

PUT is a technique for updating resources in which the client sends data that updates the whole resource

PATCH is a method of resource modification where the client transmits updated partial data without changing the complete data

The PUT method of HTTP is idempotent. As a result, each request conversion you make after retrying a request several times counts as one request

PATCH method of HTTP request is non− idempotent. Therefore, if you try the request more than once, you will end up with numerous resources, each with a distinct URI

With a PUT request, the client is asking to replace the enclosed entity, which is seen as the changed version of the resource saved on the original server

PATCH provides a set of instructions that specify how to modify a resource stored on the original server partially to produce a new version

The bandwidth of the PUT method is high

PATCH has relatively smaller bandwidth

Conclusion

Both PUT and PATCH modify already−existing data, but because of idempotency, they do it in distinct ways. Since not all servers accept PATCH, PUT is used more frequently than PATCH. Because they provide instructions to edit a record and are partial updates rather than full record replacements like a PUT request, PATCH requests might potentially lead to unanticipated outcomes.

Updated on: 12-Jul-2023

488 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements