PUT vs POST - Comparing HTTP Methods
When working with HTTP requests, you will often encounter two methods that are used to send data to a server: PUT
and POST
. These methods are similar in many ways, but they have some key differences that can make a big impact on your application's behavior. In this article, we will explore the differences between PUT
and POST
and help you decide which one is right for your needs.
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:
GET https://website.com/path/to/image.jpg
However, apart from GET
, there are other types of HTTP methods, including:
HEAD
POST
PUT
DELETE
CONNECT
OPTIONS
TRACE
Two of these methods are sometimes confused in regard to when each should be used. The two methods in question here are PUT
and POST
.
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: yourwebsite.com
Where <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: 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: 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.
PUT
vs POST
- the differences
RFC 2616, explains the difference between PUT
vs POST
as follows.
The fundamental difference between the
POST
andPUT
requests is reflected in the different meaning of the Request-URI. The URI in aPOST
request identifies the resource that will handle the enclosed entity... In contrast, the URI in aPUT
request identifies the entity enclosed with the request.
While both methods are used to send data to a server, there are some key differences between them that you should be aware of.
1. Idempotency
One of the main differences between PUT
and POST
is their 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
An HTTP method is said to be idempotent if making multiple identical requests has the same effect as making a single request. In other words, if you send the same request multiple times, you should get the same result every time.
PUT
is an idempotent method because sending the same data to the server multiple times will always result in the same resource being updated. This means that if you send a PUT
request and it fails, you can simply send the same request again and it will still have the same effect.
POST
, on the other hand, is not idempotent. Sending the same data to the server multiple times can result in multiple resources being created. For example, if you submit a form multiple times, you may end up with multiple records in the database.
2. Safety
Another difference between PUT
and POST
is their safety. An HTTP method is said to be safe if it does not modify the state of the server. In other words, sending a safe request should not change anything on the server.
PUT
is not a safe method because it modifies the state of the server. Sending a PUT
request will update or replace existing data on the server.
POST
, on the other hand, is a safe method because it does not modify the state of the server. Sending a POST
request will create new data on the server, but it will not modify any existing data.
3. Cacheability
The third difference between PUT
and POST
is their cacheability. An HTTP response can be cached by a client or a proxy server if it includes certain cache-control headers. This can help improve performance by reducing the number of requests that need to be made to the server.
PUT
is generally not cacheable because it is often used to modify existing data on the server. If a client or proxy server were to cache a PUT
request, it could cause the data to become out of date.
POST
, on the other hand, is usually cacheable. Since it creates new data on the server, caching the response will not cause any issues.
4. Request Body
Finally, there is a difference in the way that the data is sent to the server with PUT
and POST
requests. With a PUT
request, the entire resource is sent in the request body. This means that if you are updating a user account, you would need to send all of the user's details in the request body.
With POST
requests, on the other hand, typically only send the data that is being created or updated. For example, if you are submitting a form to create a new user account, you would only send the data that is required to create the account (e.g. the user's name, email, and password).
When to use PUT
Now that we've covered the differences between PUT
and POST
let's take a closer look at when you should use PUT
.
PUT
is best used when you are updating or replacing existing data on the server. This can include updating the details of a user account, changing the content of a blog post, or modifying the properties of a product in an e-commerce store.
One key advantage of using PUT
is that it is idempotent. This means that if a PUT
request fails, you can simply resend the same request, and it will have the same effect. This can be useful in situations where network connectivity is unreliable or if the client needs to retry the request for some other reason.
When you know the URL of the thing you want to create or overwrite, a PUT
method should be used.
When to use POST
Now let's look at when you should use POST
.
POST
is best used when you are creating new data on the server. This can include submitting a form to create a new user account, uploading a file, or adding a new product to an e-commerce store.
Since POST
is not idempotent, it is important to ensure that the same request is not sent multiple times. One way to do this is to include a unique identifier in the request, such as a timestamp or a nonce. This can help prevent duplicate data from being created on the server.
Another advantage of using POST
is that it is safe. Since it does not modify any existing data on the server, it is less likely to cause unintended side effects.
If you only know the URL of the category or subsection of the thing you want to create something within, use the POST
method.
Summary
In conclusion, both PUT
and POST
are important HTTP methods that are used to send data to a server. While they have some similarities, such as the way that the data is sent in the request body, they also have some key differences that can impact the behavior of your application.
PUT is best used when you are updating or replacing existing data on the server, while POST
is best used when you are creating new data. It is important to understand the differences between these two methods and to choose the one that is best suited to your specific use case.
Remember that PUT
is idempotent, while POST
is not. PUT
is also not safe, while POST
is. Finally, PUT
is generally not cacheable, while POST
is usually cacheable.
By understanding these differences and using the appropriate method for your needs, you can ensure that your HTTP requests are both efficient and effective.