Diffrence between PUT and POST HTTP requests

PUT and POST request 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

import requests
  
# Making a PUT request
r = requests.put('https://httpbin.org/put', data={'key':'value'})
  
#check status code for response recieved
# success code - 200
print(r)
  
# print content of request
print(r.content)

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

import requests
  
# Making a POST request
r = requests.post('https://httpbin.org/post', data={'key':'value'})
  
#check status code for response recieved
# success code - 200
print(r)
  
# print content of request
print(r.json())

chevron_right


save this file as request.py and through terminal run,

python request.py

Output –

post-method-python-requests

Diffrence between PUT and POST methods

PUT 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 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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.