Open In App

Http Request methods – Python requests

Improve
Improve
Like Article
Like
Save
Share
Report

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.
 

Http Request methods

Method Description
GET GET method is used to retrieve information from the given server using a given URI.
POST POST request method requests that a web server accepts the data enclosed in the body of the request message, most likely for storing it
PUT 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.
DELETE The DELETE method deletes the specified resource
HEAD The HEAD method asks for a response identical to that of a GET request, but without the response body.
PATCH It is used for modify capabilities. The PATCH request only needs to contain the changes to the resource, not the complete resource

GET

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

 

How to make GET request through Python Requests

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 request
r = requests.get('https://api.github.com / users / naveenkrnl')
  
# 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 – 
 

python-requests-get-method

For more, visit GET method – Python requests
 

POST

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. 
 

How to make POST request through Python Requests

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

For more, visit – POST method – Python requests
 

PUT

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. 
 

How to make PUT request through Python Requests

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

For more, visit – PUT method – Python requests
 

DELETE

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

 

How to make DELETE request through Python Requests

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 request
r = requests.delete('https://httpbin.org / delete', 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 –
 

delete-method-python-requests

For more, visit – DELETE method- Python requests
 

HEAD

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.
 

How to make HEAD request through Python Requests

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 request
r = requests.head('https://httpbin.org/', data ={'key':'value'})
  
# check status code for response received
# success code - 200
print(r)
  
# print headers of request
print(r.headers)
  
# checking if request contains any content
print(r.content)


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

python request.py

Output –
 

head-method-python-requestts

For more, visit – HEAD method – Python requests
 

PATCH

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.
 

How to make Patch request through Python Requests

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 request
r = requests.patch('https://httpbin.org / patch', 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 – 
 

patch-method-python-requests

For more, visit – PATCH method – Python requests
 



Last Updated : 06 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads