PUT vs POST - Comparing HTTP Methods
There are various HTTP methods that exist and each one is used for different purposes. The most popular HTTP method is the GET method which is used to retrieve data from a web server. For example, if you want to load an image from a particular website, your browser will make a request to the web server using the following command:
However, apart from GET, there are other types of HTTP methods including:
Two of these methods are sometimes confused in regards to when each should be used. The two methods in question here are PUT and POST. In this article, we’re going to talk specifically about what the difference is between PUT vs POST as well as how to properly use each method.
What Does the PUT Method Do?
The PUT method completely replaces whatever currently exists at the target URL with something else. With this method, you can create a new resource or overwrite an existing one given you know the exact Request-URI. An example of a PUT method being used to create a new resource would resemble the following:
PUT /forums/<new_thread> HTTP/2.0 Host: https://yourwebsite.com/
<new_thread> would be the actual name or ID number of the thread. Alternatively, a PUT method used to overwrite an existing resource could look like this:
PUT /forums/<existing_thread> HTTP/2.0 Host: https://yourwebsite.com/
In short, the PUT method is used to create or overwrite a resource at a particular URL that is known by the client.
What Does the POST Method Do?
The HTTP POST method is used to send user-generated data to the web server. For example, a POST method is used when a user comments on a forum or if they upload a profile picture. A POST method should also be used if you do not know the specific URL of where your newly created resource should reside. In other words, if a new forum thread is created and the thread path is not specified then you could use some like:
POST /forums HTTP/2.0 Host: https://yourwebsite.com/
Using this method, the URL path would be returned from the origin server and you would receive a response similar to:
HTTP/2.0 201 Created Location: /forums/<new_thread>
In short, the POST method should be used to create a subordinate (or child) of the resource identified by the Request-URI. In the example above, the Request-URI would be
/forums and the subordinate or child would be
<new_thread> as defined by the origin.
When to Use PUT vs POST
So, now that you know more about the difference between PUT vs POST, you should have a better idea of which one to use in certain circumstances. However, this section will aim to further clarify when to use each method.
First off, choosing between using PUT vs POST should be based on the action’s idempotence. As Wikipedia puts it,
Idempotence is the property of certain operations in mathematics and computer science, that can be applied multiple times without changing the result beyond the initial application
With this definition, we can say that the PUT method is idempotent because no matter how many times we send the same request, the results will always be the same. On the other hand, the POST method is not idempotent since if we send the same POST request multiple times, we will receive various results (i.e. a new subordinate will be created each time).
RFC 2616, explains the difference between PUT vs POST as follows.
The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity… In contrast, the URI in a PUT request identifies the entity enclosed with the request.
When you know the URL of the thing you want to create or overwrite, a PUT method should be used. Alternatively, if you only know the URL of the category or sub-section of the thing you want to create something within, use the POST method.
POST and PUT are both popular HTTP methods that may be sometimes confused or used interchangeably. However, it’s important to correctly identify the idempotence of the action at hand in order to determine whether a PUT vs POST method should be used. Otherwise, the misuse of each method may result in the occurrence of unexpected bugs.