Post vs Put

Post vs Put

Join me in discussing the differences between a POST request and a PUT request while clearing up the common confusion about them.

I once forgot how to write a POST request and that led to me thinking I could substitute in a PUT request to acquire the result I was going for.

Ladies and gents, the errors I got that day are what prompted this article placed here before you.

Life Hack: the lack of knowledge of “why” and “how” in programming will hurt you.

While researching this article, I found it intriguing that there were and are people like me who didn’t understand the proper usage of both methods. It made me more passionate about writing this article.

Before we move to the main issue, are you familiar with the term CRUD?

It is a popular remark you hear in the tech ecosystem. No matter the aspect of tech, the basic understanding of what it is, is very crucial. I assume we all know CRUD stands for Create, Read, Update, and Delete but the question is, why?

CRUD is an acronym that refers to the four basic operations executed on database applications. Create, Read, Update, and Delete are actually database commands. It was created in the 1980s to describe database functionality used by SQL and was designed as a set of functions to enhance persistent storage with a database. It is a cycle used to maintain records in a database setting.

CRUD Functionalities

  1. Create: adds one or more entries to the database. It is the equivalent of INSERT in SQL.

  2. Read: retrieves data based on certain criteria. It is the equivalent of SELECT in SQL.

  3. Update: changes or modifies records without overwriting them. It is equivalent to all the edit operations in SQL like UPDATE etc.

  4. Delete: removes one or more entries that are specified. It is like DROP and DELETE in SQL.

Through CRUD operations, Users and Administrators have the right to retrieve, create, edit, and delete records online.

Similarities Between CRUD Commands and REST Operations

CRUD commands are similar to REST operations in many ways, in fact, having an understanding of CRUD commands makes it easier to manipulate REST operations when building a RESTful API. For example:

a POST request creates a new record/resource in the database. It is similar to Create in CRUD.

a GET request reads information sourced from the database. It is similar to Read in CRUD.

a PUT request updates an object/ an existing resource. It is similar to Update in CRUD.

a DELETE request removes a record from the database. It is similar to Delete in CRUD.

Disclaimer: although CRUD and REST operations are similar they are still very much different.

What Makes a Post Request Similar to a Put Request

They both involve the addition of a new record to the database i.e. whether it involves the creation of something completely new with a POST request or the modification of an entity with a PUT request, you’re still adding something that was never there before to the database.

This is where the confusion lies. It is the reason why the usage of both requests is often misunderstood by many but I assure you, they are very different.

Differences Between a Post Request and a Put Request

Post Requests:

  1. It adds a child resource to a collection of resources.
  2. They are not idempotent. If you call a post request repeatedly, it will create the same resource multiple times.
  3. It is used for Create operations.
  4. It depicts that a web server accepts the data included in the body of the requested message, like when you send user-generated data to the web server or when you upload a file.
  5. Syntax is different from that of PUT.

Put Requests:

  1. It is used to modify a single resource that is already part of a collection of resources.
  2. If you call a put request multiple times it will produce the same result and that makes it idempotent.
  3. It is used for Update operations.
  4. Syntax is different from that of POST.

RCF-2016 clearly states that the PUT method requests for the attached entity to be stored into the server which hosts the supplied Request-URI. If the Request-URI refers to an already existing resource, an update operation will happen, otherwise, a Create operation should happen if the Request-URI is a valid resource URI (assuming the client is allowed to determine the resource identifier).

The POST method is used to request that the origin server accept the entity attached in the request as a new subordinate of the resource identified by the Request-URI in the Request-line. This means that the POST request URI should be of a collection URI.

Summary: a PUT request will only be passed successfully if the Request-URI refers to an already existing resource found in the database.

/updateexample/{example-id}

While a POST request will be passed successfully as long as the Request-URI in the Request-line is correct

/createexample

(as long as you press this link, whatever you have in the body of the request will be added to the database. If you press it five times, it will be added five times)

Authors Note:

The major difference between a POST request and a PUT request is the reason for usage. What problem are you trying to solve? What is the result that you’re hoping for? The major difference between both requests is the situation in front of you. That alone determines which is more appropriate.

Just because a senior dev says you can just use a PUT request instead of a POST request doesn’t mean it’ll work for you. It’s your function, deal with it the way you know how to. Senior devs are there to guide you but don’t take all their bits of advice to be god-tier. Take your time and learn.

A different error message equates to progress you know✨✨.

If you haven’t subscribed to our newsletter click here

Until next time user.

a post request fighting a put request