The Simple Guide to HTTP Verbs: PATCH, PUT, and POST
The next time you design a RESTful API or web app, you'll likely incorporate HTTP verbs. It's important to understand the subtle differences between PATCH, PUT, and POST.
Join the DZone community and get the full member experience.
Join For FreeThe terms PATCH, PUT, and POST are often confused with each other. Many resources promote the concept of CRUD (create, read, update, delete) applications and tie HTTP verbs into a single part of the process. The reality is far more complex than a short acronym, especially as you run into overlapping functionality and other complications.
The differences between them are subtle but can make significant changes in the way you create and update resources. It's time to shed some light on PATCH, PUT, and POST, and when you should use each.
POST
You use POST to create a resource and instruct the server to make a Uniform Resource Identifier (URI) for it. For example, when you want to create a new article you would POST to /articles to make the file and get the URI, so you end up with /articles/1234/.
PUT
PUT also creates resources, but it does so for a known URI. So, you can PUT to /articles/1234/. If the article doesn't exist, PUT creates it. If it does exist, this HTTP verb updates it. While PUT seems nearly identical to POST, the difference between the two comes down to idempotence.
Idempotence is a property that creates identical side effects whether you have one or many results. PUT has this characteristic, while POST creates new resources infinitely. In general, POST works best for resource creation, while PUT handles updates.
PATCH
So, where does PATCH come into the picture? This HTTP verb partially updates resources without sending the complete representation of it. When you're working with complicated objects and resources, it's a big deal to update more than you need to.
With this example...
{ "first_name": "Claude", "last_name": "Elements", "email": "claude@cloud-elements.com", "favorite_color": "blue" }
...PATCH allows you to change the color with JSON
:{ "favorite_color": "purple" }
.
The next time you design a RESTful API or build a new web application, you will probably incorporate HTTP verbs. Understanding the subtle differences between PUT, POST, and PATCH can have a significant effect on the way you use these verbs. Overall, you improve your experience when integrating and building cooperative apps.
Opinions expressed by DZone contributors are their own.
Comments