The Wayback Machine - https://web.archive.org/web/20240915133334/https://www.geeksforgeeks.org/http-request-methods-python-requests/
Open In App

Http Request methods – Python requests

Last Updated : 06 Aug, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
 



Similar Reads

Flask HTTP methods, handle GET & POST requests
In this article, we are going to learn about how to handle GET and POST requests of the flask HTTP methods in Python. HTTP Protocol is necessary for data communication. In the context of the World Wide Web, an HTTP method is a request method that a client (e.g. a web browser) can use when making a request to a server (e.g. a web server). There are
6 min read
GET and POST Requests in GraphQL API using Python requests
In this article, we will be understanding how to write GET and POST requests to GRAPHQL APIs using the Python request module. Dealing with GraphQL API is a bit different compared to the simple REST APIs. We have to parse in a query that involves parsing the GraphQL queries in the request body. What are Requests in Python Requests is a python packag
9 min read
response.request - Python requests
response.request returns the request object that requested this response. Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain features such as content, headers, etc
2 min read
Python requests - POST request with headers and body
HTTP headers let the client and the server pass additional information with an HTTP request or response. All the headers are case-insensitive, headers fields are separated by colon, key-value pairs in clear-text string format. Request with headersRequests do not change its behavior at all based on which headers are specified. The headers are simply
4 min read
Network Programming Python - HTTP Requests
HTTP stands for HyperText Transfer Protocol, which works on the client-server machine. In most cases, the web browser acts as the client, and the computer which hosts the website acts as a server. Python provides the requests module to play with HTTP requests. The requests module plays a major role in HTTP requests, apart from simple request and th
2 min read
Asynchronous HTTP Requests with Python
Performing multiple HTTP requests is needed while working with applications related to data preprocessing and web development. In the case of dealing with a large number of requests, using synchronous requests cannot be so efficient because each request must wait for the previous request to get completed. Hence, this approach delays significantly,
4 min read
Making HTTP Requests from a FastAPI Application to an External API
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints. It is designed to be easy to use, efficient, and reliable, making it a popular choice for developing RESTful APIs and web applications. Your FastAPI server may need to fetch data from other external APIs, to perform da
4 min read
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 RequestHTTP PUT is a request method supported by HT
3 min read
Response Methods - Python requests
When one makes a request to a URI, it returns a response. This Response object in terms of python is returned by requests.method(), method being - get, post, put, etc. Response is a powerful object with lots of functions and attributes that assist in normalizing data or creating ideal portions of code. For example, response.status_code returns the
5 min read
HEAD method - Python requests
Requests library is one of the important aspects of Python for making HTTP requests to a specified URL. This article revolves around how one can make HEAD request to a specified URL using requests.head() method. Before checking out the HEAD method, let's figure out what a Http HEAD request is - HEAD Http Method HEAD is a request method supported by
2 min read
DELETE method- Python requests
Requests library is one of the important aspects of Python for making HTTP requests to a specified URL. This article revolves around how one can make DELETE request to a specified URL using requests.delete() method. Before checking out the DELETE method, let's figure out what a Http DELETE request is - DELETE Http Method DELETE is a request method
2 min read
PUT method - Python requests
Requests library is one of the important aspects of Python for making HTTP requests to a specified URL. This article revolves around how one can make PUT request to a specified URL using requests.put() method. Before checking out the PUT method, let's figure out what a Http PUT request is - PUT Http Method PUT is a request method supported by HTTP
3 min read
PATCH method - Python requests
Requests library is one of the important aspects of Python for making HTTP requests to a specified URL. This article revolves around how one can make PATCH request to a specified URL using requests.patch() method. Before checking out the PATCH method, let's figure out what a Http PATCH request is - PATCH Http Method PATCH is a request method suppor
3 min read
POST method - Python requests
Requests library is one of the important aspects of Python for making HTTP requests to a specified URL. This article revolves around how one can make POST request to a specified URL using requests.post() method. Before checking out the POST method, let's figure out what a POST request is - POST Http Method POST is a request method supported by HTTP
2 min read
GET method - Python requests
Requests library is one of the important aspects of Python for making HTTP requests to a specified URL. This article revolves around how one can make GET request to a specified URL using requests.GET() method. Before checking out GET method, let's figure out what a GET request is - GET Http Method The GET method is used to retrieve information from
2 min read
response.headers - Python requests
Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain features such as content, headers, etc. This article revolves around how to check the response.headers out of a
2 min read
response.elapsed - Python requests
Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain features such as content, headers, etc. This article revolves around how to check the response.elapsed out of a
2 min read
response.close() - Python requests
Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain features such as content, headers, etc. This article revolves around how to check the response.close() out of a
2 min read
response.content - Python requests
Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain features such as content, headers, etc. This article revolves around how to check the response.content out of a
2 min read
response.cookies - Python requests
Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain features such as content, headers, etc. This article revolves around how to check the response.cookies out of a
2 min read
response.history - Python requests
Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain features such as content, headers, etc. This article revolves around how to check the response.history out of a
2 min read
response.is_permanent_redirect - Python requests
Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain features such as content, headers, etc. This article revolves around how to check the response.is_permanent_redi
2 min read
response.is_redirect - Python requests
Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain features such as content, headers, etc. This article revolves around how to check the response.is_redirect out o
2 min read
response.iter_content() - Python requests
response.iter_content() iterates over the response.content. Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain features such as content, headers, etc. This article
2 min read
response.url - Python requests
response.url returns the URL of the response. It will show the main url which has returned the content, after all redirections, if done. Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would b
2 min read
response.text - Python requests
response.text returns the content of the response, in unicode. Basically, it refers to Binary Response content. Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain
2 min read
response.status_code - Python requests
response.status_code returns a number that indicates the status (200 is OK, 404 is Not Found). Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain features such as
2 min read
response.reason - Python requests
response.reason returns a text corresponding to the status code. for example, OK for 200, Not Found for 404. Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain fea
2 min read
response.raise_for_status() - Python requests
response.raise_for_status() returns an HTTPError object if an error has occurred during the process. It is used for debugging the requests module and is an integral part of Python requests. Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns
2 min read
response.ok - Python requests
response.ok returns True if status_code is less than 400, otherwise False. Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain features such as content, headers, et
2 min read
Article Tags :
Practice Tags :