Http Request methods – Python requests

06 Aug, 2021
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 server. A web browser may be the client, and an application on a computer that hosts a web site may be the server. This article revolves around various methods that can be used to make a request to a specified URI. 
GET method is used to retrieve information from the given server using a given URI. The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ‘?’ character. For example: 
https://www.google.com/search?q=hello
 
Python’s requests module provides in-built method called get() for making a GET request to a specified URI.Syntax –  
requests.get(url, params={key: value}, args)
Example – Let’s try making a request to github’s APIs for example purposes.  
Python3
import requests   # Making a GET requestr = requests.get('https://api.github.com / users / naveenkrnl')  # check status code for response received# success code - 200print(r)  # print content of requestprint(r.content)
save this file as request.py and through terminal run,  
python request.py
Output –  
For more, visit GET method – Python requests 
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.  
Python’s requests module provides in-built method called post() for making a POST request to a specified URI.Syntax –  
requests.post(url, params={key: value}, args)
Example – Let’s try making a request to httpbin’s APIs for example purposes.  
Python3
import requests  # Making a POST requestr = requests.post('https://httpbin.org / post', data ={'key':'value'})  # check status code for response received# success code - 200print(r)  # print content of requestprint(r.json())
save this file as request.py and through terminal run,  
python request.py
Output – 
For more, visit – POST method – Python requests 
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.  
Python’s requests module provides in-built method called put() for making a PUT request to a specified URI.Syntax –  
requests.put(url, params={key: value}, args)
Example – Let’s try making a request to httpbin’s APIs for example purposes.  
Python3
import requests  # Making a PUT requestr = requests.put('https://httpbin.org / put', data ={'key':'value'})  # check status code for response received# success code - 200print(r)  # print content of requestprint(r.content)
save this file as request.py and through terminal run,  
python request.py
Output – 
For more, visit – PUT method – Python requests 
DELETE is a request method supported by HTTP used by the World Wide Web. The DELETE method deletes the specified resource. As with a PUT request, you need to specify a particular resource for this operation. A successful response SHOULD be 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has not yet been enacted, or 204 (No Content) if the action has been enacted but the response does not include an entity. An example URI looks like for delete operation 
http://www.example.com/articles/12345
 
Python’s requests module provides in-built method called delete() for making a DELETE request to a specified URI.Syntax –  
requests.delete(url, params={key: value}, args)
Example – Let’s try making a request to httpbin’s APIs for example purposes.  
Python3
import requests  # Making a DELETE requestr = requests.delete('https://httpbin.org / delete', data ={'key':'value'})  # check status code for response received# success code - 200print(r)  # print content of requestprint(r.json())
save this file as request.py and through terminal run,  
python request.py
Output – 
For more, visit – DELETE method- Python requests 
HEAD is a request method supported by HTTP used by the World Wide Web. The HEAD method asks for a response identical to that of a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content. 
Python’s requests module provides in-built method called head() for making a HEAD request to a specified URI.Syntax –  
requests.head(url, params={key: value}, args)
Example – Let’s try making a request to httpbin’s APIs for example purposes.  
Python3
import requests  # Making a HEAD requestr = requests.head('https://httpbin.org/', data ={'key':'value'})  # check status code for response received# success code - 200print(r)  # print headers of requestprint(r.headers)  # checking if request contains any contentprint(r.content)
save this file as request.py and through terminal run,  
python request.py
Output – 
For more, visit – HEAD method – Python requests 
PATCH is a request method supported by HTTP used by the World Wide Web. It is used for modify capabilities. The PATCH request only needs to contain the changes to the resource, not the complete resource. This resembles PUT, but the body contains a set of instructions describing how a resource currently residing on the server should be modified to produce a new version. This means that the PATCH body should not just be a modified part of the resource, but in some kind of patch language like JSON Patch or XML Patch. PATCH is neither safe nor idempotent. 
Python’s requests module provides in-built method called patch() for making a PATCH request to a specified URI.Syntax –  
requests.patch(url, params={key: value}, args)
Example – Let’s try making a request to httpbin’s APIs for example purposes.  
Python3
import requests  # Making a PATCH requestr = requests.patch('https://httpbin.org / patch', data ={'key':'value'})  # check status code for response received# success code - 200print(r)  # print content of requestprint(r.content)
save this file as request.py and through terminal run,  
python request.py
Output –  
For more, visit – PATCH method – Python requests 
abhishek0719kadiyan
Python-requests
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.