5

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).

Community
  • 1
  • 1
Adam Zerner
  • 17,797
  • 15
  • 90
  • 156
  • 1
    Maybe I don't want to take the diff and deduce where I should save it so that everything makes sense. Maybe I just want to work with full resources instead of messing around with little parts of them. – fps Mar 03 '15 at 00:38

4 Answers4

4

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

Community
  • 1
  • 1
ma499
  • 601
  • 4
  • 13
  • However, below we have "A PATCH request can be issued in such a way as to be idempotent, which also helps prevent bad outcomes from collisions between two PATCH requests on the same resource in a similar time frame." – vectorialpx Jan 12 '23 at 11:57
  • That's true - although it depends on the server implementation offering a conditional mechanism (e.g. Last-Modified or E-Tag header). – ma499 Jan 23 '23 at 09:53
2

You might want to create the resource, or there might not be an applicable PATCH format available (think binary files).

Julian Reschke
  • 40,156
  • 8
  • 95
  • 98
0
  • with using POST as only option to create resources
  • only having JSON resources (RFC 7396)
  • making your PATCH endpoint implementation idempotent

Is there still a need to provide a PUT endpoint in an API?

Maybe I don't want to take the diff and deduce where I should save it so that everything makes sense. Maybe I just want to work with full resources instead of messing around with little parts of them. >> you can use PATCH for full update as well, no?

Community
  • 1
  • 1
Frank
  • 9
  • 2
  • it would be great if you add some documentation references to your answer, which will help SO members. – dkb Sep 24 '18 at 05:47
-1

A considerable part of the HTTP standard is outdated, so it is not surprising that PATCH can completely replace PUT.

In RESTful API, people often mistakenly regard PUT as "update the entire resource", but in fact the semantics of PUT is "replace the resource". Unfortunately, there is a sad design in the HTTP: PUT is defined to create new resources when they don't exist, so PUT is sometimes used as an idempotent alternative to POST.

PATCH is an imitation of PUT to a certain extent, so it also incorporates part of the responsibilities of POST. However, if you only want to have an action of "modify the existing resource", you only need to use PATCH (PATCH is not idempotent, another sad design of HTTP).

Those who study REST and HTTP standard methods in depth will eventually find that a simple RPC interface can better implement all the actions you need.

BlackGlory
  • 3,866
  • 2
  • 15
  • 21
  • "A considerable part of the HTTP standard is outdated"? Please explain. – Julian Reschke Aug 30 '20 at 09:26
  • @JulianReschke Just find out why RFC 2616 was replaced. Nowadays, most HTTP standards are inherited from RFC 2616. They are designed for old Internet application scenarios. PATCH (RFC 5789) was formulated in 2010 to make up for the shortcomings of PUT. Your request is like asking why people need something new. – BlackGlory Aug 30 '20 at 11:13
  • But RFC 2616 is *not* the "HTTP standard". It has been obsoleted six years ago (by RFCs 723*). If you see an out-of-date problem with *these*, now would be a good time to report it, as they are currently revised. See . – Julian Reschke Aug 31 '20 at 07:30
  • @JulianReschke You may be right, HTTP 1.1 is not the "HTTP standard", but I can’t ignore the fact that a backward-compatible specification that was created decades ago is no longer suitable for modern applications. If the IETF working group is willing to remove the semantics of PUT about creating new resources, or change PATCH to idempotent, then I am happy to provide advice, otherwise the facts are just facts. – BlackGlory Aug 31 '20 at 09:40
  • "a backward-compatible specification that was created decades ago is no longer suitable for modern applications" - details, please."willing to remove the semantics of PUT about creating new resourceswilling to remove the semantics of PUT about creating new resources" - that would be an incompatible change. – Julian Reschke Aug 31 '20 at 10:18
  • FWIW, "PUT for create" makes sense when the client wants/can control the URI of the resource to be created. There's nothing wrong with that. – Julian Reschke Aug 31 '20 at 10:21
  • @JulianReschke "That would be an incompatible change" is the main reason for the obsolescence of the HTTP standard. Maybe I should change the wording, because it is not out of date, it is wrong. "makes sense when the client wants/can control the URI of the resource to be created", that is wrong. POST can do exactly the same thing, as long as you provide the same URI as PUT (you will find idempotence is actually related to the URIs or payloads, not methods, another sad design). PUT is meaningless, it is update + create = upsert, HTTP methods lack the UPDATE method before PATCH. – BlackGlory Aug 31 '20 at 12:07
  • But PATCH is also a sad design, unfortunately, HTTP is a mess. – BlackGlory Aug 31 '20 at 12:10
  • Well, you seem to have a different understanding of "obsolescence" (and also how internet standards can be involved) than I do. Enough said. – Julian Reschke Aug 31 '20 at 12:53
  • @JulianReschke I guess you just don't understand that IETF can actually abandon compatibility with the old HTTP standard. Anyway, this discussion is not bad. – BlackGlory Aug 31 '20 at 13:07
  • "that IETF can actually abandon compatibility" - actually, no, in practice it can't. If you really want to lead this discussion, you might want to raise the point on the IETF HTTPBIS WG mailing list (see https://lists.w3.org/Archives/Public/ietf-http-wg/) – Julian Reschke Aug 31 '20 at 13:42
  • @JulianReschke It doesn't matter, in 50 years we will still use the sad POST, PUT, PATCH, and related questions on this website will continue to get upvotes from new developers. – BlackGlory Aug 31 '20 at 14:00