Python Requests Module

The python requests module is useful to perform the HTTP requests like GET, POST, DELETE, etc., to get/post data to the specified URL.

 

Before you start using the requests module, you need to install the requests module in the PIP location. To download and install the requests module, open your command prompt, and navigate your PIP location and type the pip install requests command.

 

C:\Program Files\Python38\Scripts>pip install requests

After completion of installing the requests module, your command-line interface will be as shown below.

 

Python pip install requests module

 

If you already open the python interpreter, close it and reopen the interpreter to sync with the latest changes. After installing the requests module, you can use it in your applications by importing the requests module, as shown below.

 

import requests

Following is the example of using the requests module to send the HTTP GET request to the specified URL in python.

 

import requests

result = requests.get('https://api.github.com')
if result.status_code == 200:
    print('Success')
elif result.status_code == 404:
    print('Not Found')

The above requests module example will print the response based on the result object that we received from the HTTP request.

 

When you execute the above python example, it will return the result as shown below.

 

Success

The response object that we received from the HTTP request will have different properties/methods like status_code, content, text, etc., to perform different operations based on our requirements.

 

Property/MethodDescription
status_code It will return the status code that will indicate the status of a request.
content It will return the response content in bytes.
text It will return the response content in Unicode format.
ok It will return True if status_code is less than 400 otherwise False.
reason It will return the text based on the status code.
request It will return the request object that is used to get the response.
headers It will return the response headers in dictionary format.
url It will return the response URL.
links It will return the header links.
next It will return the PreparedRequest object for the next request in a redirection.
apparent_encoding It will return the apparent encoding.
cookies It will return the cookiejar object with the cookies that were received from the server.
elapsed It will return the time elapsed during sending the request and receiving the response.
encoding It will return the encoding that is used to decode the response text.
history It will return the list of response objects holding the history of the request.
is_permanent_redirect It will return True if the response is the permanent redirected url otherwise, False.
is_redirect It will return True if the response is redirected otherwise, False.
raise_for_status() If any error occurs, it will return the HTTPError object.
iter_content() It will iterate over the response.
iter_lines() It will iterate over the lines of response.
json() It will return the response object in dictionary format.
close() It is useful to close the server connection.

Like HTTP GET method, the request module has different methods to perform HTTP operations based on your requirements.

 

MethodDescription
get(url, params, args) Useful to make GET request to the specified URL.
post(url, data, json, args) Useful to make a POST request to the specified URL.
put(url, data, args) Useful to make a PUT request to the specified URL.
patch(url, data, args) Useful to send a PATCH request to the specified URL.
delete(url, args) Useful to send a DELETE request to the specified URL.
options(url, args) Useful to make an OPTIONS request to the specified URL.
head(url, args) Useful to make a HEAD request to the specified URL and return the HTTP headers.
request(method, url, args) Useful to request the specified method to the specified URL.

Following is the example of using the different HTTP methods in the requests module.

 

import requests as rq

r1 = rq.get('https://api.github.com/search/repositories', params={'q': 'requests+language:python'})
print(r1)
r2 = rq.post('https://httpbin.org/post', data={'key':'value'})
print(r2)
r3 = rq.put('https://httpbin.org/put', data={'key':'value'})
print(r3)
r4 = rq.delete('https://httpbin.org/delete')
print(r4)
r5 = rq.head('https://httpbin.org/get')
print(r5)
r6 = rq.patch('https://httpbin.org/patch', data={'key':'value'})
print(r6)
r7 = rq.options('https://httpbin.org/get')
print(r7)

The HTTP GET method will send the parameters in the query string using params parameter. The POST, PUT, and PATCH methods will send the payload through the message body using data parameter.

 

This is how you can use the requests module in python to get the required data from other URLs using HTTP methods such as GET, POST, PUT, PATCH, etc., based on your requirements.