DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Offline-First Patch Management for 10,000 Edge Nodes: A Practical Architecture That Scales
  • Applying Oracle 19c Release Update (RU): A Practical Guide from My DBA Experience
  • IT Asset, Vulnerability, and Patch Management Best Practices
  • Zero-Touch Patch Management With PowerShell and Intune: How We Automated Compliance at Scale

Trending

  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • 7 Technology Waves I’ve Seen in 30 Years of Software — Will AI Be the Next Real Transformation?
  • Good Data, Bad Metric: A Mutation Testing Pattern for Analytics Engineering
  • Rust-Native Alternatives to Spark SQL and DataFrame Workloads
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Demystifying HTTP Verbs: Patch, Put, and Post

Demystifying HTTP Verbs: Patch, Put, and Post

Which verbs should you use and when should you use them?

By 
Brian Busch user avatar
Brian Busch
·
Dec. 18, 20 · Opinion
Likes (3)
Comment
Save
Tweet
Share
8.7K Views

Join the DZone community and get the full member experience.

Join For Free

If you’re still getting the hang of web development and HTTP specification, you might find yourself often confused about which verbs to use and when. 

Some developers learn that most applications on the internet are CRUD (Create, Read, Update, Delete) applications and that the HTTP verbs match to these actions 1:1. POST is a Create, GET is a Read, PATCH (or PUT) is an Update, and DELETE is a Delete.

But unfortunately, nothing is that easy - such is life. For instance, a PUT can be used to both create and update a resource. But...if a PUT can create a resource, why would I use POST? And which method is better for updating - PUT or PATCH? 

Let’s dive in and discuss five of the most common HTTP verbs and when to use them (GET, POST, PATCH, PUT, and DELETE.)

The History of HTTP Verbs

When the HTTP/1.1 specification was first released in 1999, it established four of the five main HTTP verbs we still use today – GET, POST, PUT, and DELETE.

But it wasn’t until 2010 that RFC 5789 introduced the PATCH verb as a way of partially updating a resource. So before we even start to think about PATCH, what’s the difference between PUT and POST? A POST is used when you want to both create a resource AND have the server create a URI (Uniform Resource Identifier) for said resource.

For instance, if you wanted to create a new article, you would need to perform a POST to /articles and the article would be created and given a URI (for example, /articles/1234.) 

But how do you create a resource via PUT? When using PUT, we expect to already KNOW the URI of the resource. So if I perform a PUT to /articles/1234 and it does not already exist, it will create that resource for me at that URI. I can also perform a PUT to /articles/1234 if it DOES exist, and it will update the resource.

It may seem like a minor difference, but it’s an important distinction. The HTTP specification considers PUT to be “idempotent,” while POST is not.

The definition of idempotence: “Methods can have the property of “idempotence” in that (aside from error or expiration issues) the side-effects of N > 0 identical requests is the same as for a single request. The methods GET, HEAD, PUT, and DELETE share this property.”

JSON Object Examples

Because PUT is idempotent, this means that you can execute the same PUT request with the same data to /articles/1234 infinite times and expect the same result or side effects. If you were to execute a POST to /articles with the same data infinite times, you would get infinite new resources. 

Basically, you use POST to create a new resource when you don’t know the URI. You should use PUT to create a new resource when you DO know the URI or if you want to update a resource.

So why introduce PATCH? PATCH was introduced for partial resource updating. Let’s take the following JSON object example for a user:

{ "first_name": "Claude", "last_name": "Elements", "email":

"[email protected]", "favorite_color": "blue" }

If you wanted to update the favorite_color attribute via a PUT, you would need to send an entire representation of the resource and just change the favorite_color. For this example object it’s no big deal, but for larger, more complicated objects or resources it can be tedious. With a PATCH, you could simply send the following JSON to update the favorite color:

{ "favorite_color": "red" }

Once again, like when comparing POST and PUT, this is a very subtle but important distinction.

When designing your next RESTful API or building your next web application, you will almost certainly use HTTP and its associated verbs. Selecting the right verbs is an important part of that process. The more we all understand and follow the specifications, the better our experience will be when integrating and building cooperative apps.

Ready to learn more about best practices for building an API integration? Check out our Definitive Guide to API Integration (you might find the “Develop” and “API Design” sections especially useful.)

POST (HTTP) Patch (computing)

Published at DZone with permission of Brian Busch. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Offline-First Patch Management for 10,000 Edge Nodes: A Practical Architecture That Scales
  • Applying Oracle 19c Release Update (RU): A Practical Guide from My DBA Experience
  • IT Asset, Vulnerability, and Patch Management Best Practices
  • Zero-Touch Patch Management With PowerShell and Intune: How We Automated Compliance at Scale

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook