Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Difference between PUT and POST HTTP requests

  • Last Updated : 31 Aug, 2021

PUT and POST requests have lots of similarities certainly when making an HTTP request and both can be meddled with so that one performs functions of the other. This article revolves around the major differences between PUT and POST Requests.
 

PUT HTTP Request

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 URI 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. 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Python3






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 terminal run, 
 

python request.py

Output 
 

put-request-pytohn-requests

 POST HTTP Request

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. 
 

Python3




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 terminal run, 
 

python request.py

Output –
 



post-method-python-requests

 Difference between PUT and POST methods

PUTPOST

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 client 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 of a collection URI. 
Example – 
 

POST /articles

 

PUT method is idempotent. So if you send retry a request multiple times, that should be equivalent to 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 server.
Use PUT when you want to modify a single resource which is already a part of resources collection. PUT overwrites the resource in its entirety. Use PATCH if 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.

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!