Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would you use PUT instead of PATCH?

From what I understand, PUT requests send the whole object while PATCH requests just send the diff, which is used to update the object in the database.

Why would you do a PUT over a PATCH? PATCH seems much lighter. I don't see any upsides to PUT (I'm sure they exist, I just don't know what they are).

like image 355
Adam Zerner Avatar asked Sep 29 '22 15:09

Adam Zerner


1 Answers

A better way of looking at is that PUT replaces a resource, whilst PATCH is for providing an instruction to change a resource.

Replacing a resource is always a safe and idempotent operation as it has no dependency on the existing state of the resource. Meanwhile a request to change a resource may be dependent on the state of that resource and can therefore have other effects.

The HTTP PATCH verb is defined in RFC 5789, which states:

The difference between the PUT and PATCH requests is reflected in the way the server processes the enclosed entity to modify the resource identified by the Request-URI. 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 replaced. With 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. The PATCH method affects the resource identified by the Request-URI, and it also MAY have side effects on other resources; i.e., new resources may be created, or existing ones modified, by the application of a PATCH.

It goes on to say:

PATCH is neither safe nor idempotent

like image 160
ma499 Avatar answered Oct 05 '22 07:10

ma499