Difference between PUT and POST HTTP requests
Last Updated :
11 Jun, 2024
Pre-Requisite: Hyper Text Transfer Protocol
PUT and POST requests have lots of similarities certainly when making an HTTP request and both can be meddled with so that one performs the functions of the other. This article revolves around the major differences between PUT and POST Requests.
HTTP PUT Request
HTTP PUT is a request method supported by HTTP used by the World Wide Web. The PUT method requests that the enclosed entity be stored under the supplied URI. If the URL refers to an already existing resource, it is modified and if the URI does not point to an existing resource, then the server can create the resource with that URI.
Example: Let's try making a request to httpbin's APIs for example purposes.
Python
import requests
# Making a PUT request
r = requests.put('https://httpbin.org/put', data={'key':'value'})
#check status code for response received
# success code - 200
print(r)
# print content of request
print(r.content)
save this file as request.py and through the terminal run,
python request.py
Output:
HTTP PUT RequestAdvantages of HTTP PUT
- HTTP PUT helps you in creating as many resources as you want to create.
- HTTP PUT helps you in creating new resources very easily.
- HTTP PUT helps in storing the supplied entity with the URL.
- HTTP PUT keeps you free from checking whether multiple submit buttons have been clicked or not.
HTTP POST Request
HTTP POST is a request method supported by HTTP used by the World Wide Web. By design, the POST request method requests that a web server accepts the data enclosed in the body of the request message, most likely for storing it. It is often used when uploading a file or when submitting a completed web form.
Example: Let's try making a request to httpbin's APIs for example purposes.
Python
import requests
# Making a POST request
r = requests.post('https://httpbin.org/post', data={'key':'value'})
#check status code for response received
# success code - 200
print(r)
# print content of request
print(r.json())
save this file as request.py and through the terminal run,
python request.py
Output:
HTTP POST RequestAdvantages of HTTP POST
- HTTP POST method helps in finding the URL of the resource.
- HTTP POST is a more reliable method as it is not present in browser history.
- HTTP POST helps transmit large amounts of data.
- HTTP POST can keep the data private which helps in better security of the data.
Difference between HTTP PUT and HTTP POST Methods
HTTP PUT | HTTP POST |
---|
PUT request is made to a particular resource. If the Request-URI refers to an already existing resource, an update operation will happen, otherwise create operation should happen if Request-URI is a valid resource URI (assuming the POST-request-URIclient is allowed to determine resource identifier).
Example: PUT /article/{article-id}
| POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line. It essentially means that POST-request-URI should be a collection URI.
Example: POST /articles
|
That method is idempotent. So if you send retry a request multiple times, that should be equivalent to a single request modification.
| POST is NOT idempotent. So if you retry the request N times, you will end up having N resources with N different URIs created on the server.
|
Use PUT when you want to modify a single resource that is already a part of the resources collection. PUT overwrites the resource in its entirety. Use PATCH if the request updates part of the resource.
| Use POST when you want to add a child resource under resources collection.
|
Generally, in practice, always use PUT for UPDATE operations.
| Always use POST for CREATE operations.
|
Similar Reads
Flask HTTP methods, handle GET & POST requests In this article, we are going to learn about how to handle GET and POST requests of the flask HTTP methods in Python. HTTP Protocol is necessary for data communication. In the context of the World Wide Web, an HTTP method is a request method that a client (e.g. a web browser) can use when making a r
6 min read
GET and POST Requests Using Python This post discusses two HTTP (Hypertext Transfer Protocol) request methods  GET and POST requests in Python and their implementation in Python. What is HTTP? HTTP is a set of protocols designed to enable communication between clients and servers. It works as a request-response protocol between a cli
7 min read
Python requests - POST request with headers and body HTTP headers let the client and the server pass additional information with an HTTP request or response. All the headers are case-insensitive, headers fields are separated by colon, key-value pairs in clear-text string format.Request with headersRequests do not change its behavior at all based on wh
4 min read
POST method - Python requests Requests library is one of the important aspects of Python for making HTTP requests to a specified URL. This article revolves around how one can make POST request to a specified URL using requests.post() method. Before checking out the POST method, let's figure out what a POST request is -Â Â POST Ht
2 min read
PUT method - Python requests The requests library is a powerful and user-friendly tool in Python for making HTTP requests. The PUT method is one of the key HTTP request methods used to update or create a resource at a specific URI.Working of HTTP PUT Method If the resource exists at the given URI, it is updated with the new dat
2 min read
Http Request methods - Python requests Python requests module has several built-in methods to make Http requests to specified URI using GET, POST, PUT, PATCH or HEAD requests. A Http request is meant to either retrieve data from a specified URI or to push data to a server. It works as a request-response protocol between a client and serv
7 min read
PATCH method - Python requests Requests library is one of the important aspects of Python for making HTTP requests to a specified URL. This article revolves around how one can make PATCH request to a specified URL using requests.patch() method. Before checking out the PATCH method, let's figure out what a Http PATCH request is -
3 min read
GET and POST in Python CGI In this article, we will explore the GET and POST methods in CGI. In Python, the Common Gateway Interface (CGI) plays an important role as it enables and helps in the creation of dynamic web pages. It basically allows Python scripts to be executed and run on a web server in response to a client's HT
5 min read
Network Programming Python - HTTP Requests HTTP stands for HyperText Transfer Protocol, which works on the client-server machine. In most cases, the web browser acts as the client, and the computer which hosts the website acts as a server. Python provides the requests module to play with HTTP requests. The requests module plays a major role
2 min read