Open In App

Difference between PUT and POST HTTP requests

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. 






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 Request

Advantages of HTTP PUT

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.




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 Request

Advantages of HTTP POST

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.


Article Tags :