Table of contents
1.
Introduction
2.
What is the Request Library
3.
Getting Started With The Requests Module
4.
Making Request
4.1.
GET request
4.2.
Status Code
4.3.
Content
4.4.
Header
4.5.
Other requests
5.
FAQs
6.
Key Takeaways
Last Updated: Aug 13, 2025

Requests Module in Python

Author APURV RATHORE
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In any programming language, dealing with HTTP requests is a complex problem. When it comes to handling HTTP-related operations, Python has two built-in modules, urllib and urllib2. Both modules have their own set of capabilities, and they are frequently used in combination. The most significant disadvantage of using urllib is that it is complicated (just a few methods are accessible in both urllib and urllib2), the documentation is unclear, and writing even a simple HTTP request requires a lot of code.

To make things easier, there is an easy-to-use third-party library called Requests that most developers choose to use instead of urllib/urllib2.

What is the Request Library

The Requests library is an essential part of Python that allows you to make HTTP requests to a particular URL. Python requests have built-in management features for both the request and the response. A response is returned when a request is made to a URI. 

Request library hides the difficulties of making requests behind a beautiful, straightforward API, allowing you to concentrate on interacting with services and consuming data in your app. Requests library must be learned before learning REST APIs or Web Scraping technologies. 

Getting Started With The Requests Module

First, we need to install the requests module on our computer. To do so, execute the following command in the terminal. 

pip install requests
You can also try this code with Online Python Compiler
Run Code

 

Now we can successfully import the module using.

import requests
You can also try this code with Online Python Compiler
Run Code

Making Request

GET request

When making an HTTP request, HTTP methods like GET and POST define the type of action you're trying to do. There are numerous additional popular methods that the request library supports.

The GET method is one of the most often used HTTP methods. The GET method shows that you're attempting to obtain data from a certain site. To invoke GET request, use request. get().

The below example shows how to invoke a get request to GitHub API. 

>>>requests.get('https://api.github.com/events')
<Response [200]>
You can also try this code with Online Python Compiler
Run Code

A Response is a useful object for examining the request's outcomes. Let's repeat the request, but this time save the response value in a variable so you can examine its properties and behaviors more closely. 

>>>r = requests.get('https://api.github.com/events')
You can also try this code with Online Python Compiler
Run Code

 

We’ve saved the return value of get(), which is an instance of Response, in a variable named rin our example. We may now view a lot of information about the results of your GET request by using response.

Status Code

The status code is the most crucial information we may get from Response. The request's status is indicated by a status code. A 200 OK status, for example, means that your request was successful, but a 404 NOT FOUND status indicates that the resource you requested was not found. There is a slew of different status codes that might help you figure out what went wrong with your request.

We can access the status code by using the following command. 

>>> r.status_code
200
You can also try this code with Online Python Compiler
Run Code

 

If we try to access an invalid API, we will get a 404 status code. 

>>> r = requests.get('https://api.github.com/abc')
>>> r
<Response [404]>
You can also try this code with Online Python Compiler
Run Code

Content

There is some useful information known as a payload in the message body of a GET request response. You may view the payload in a number of forms by using the properties and methods of Response.

You may use .content: to see the response's content in bytes.

>>> r = requests.get('https://api.github.com/events')
>>> r.content
b'[{"id":"20366790391","type":"CreateEvent","actor":{"i……
You can also try this code with Online Python Compiler
Run Code

 

We can also convert it into text form for readability by using .text as shown in the example below. 

>>> r.text
You can also try this code with Online Python Compiler
Run Code

 

The response is actually serialised JSON content. To get a dictionary, we can simply use the below command.

>>> r.json()
You can also try this code with Online Python Compiler
Run Code

 

Because the return value of.json() is a dictionary, you may retrieve the object's values by key.

With status codes and message bodies, you can accomplish a lot. However, if you want further information, such as metadata about the response, you have to examine the response's headers.

Header

The response headers can include important information such as the response payload's content type and a time limit for caching the response. r.headers to see these headers:

>>> r.headers
{'Server': 'GitHub.com', 'Date': 'Mon, 21 Feb 2022 07:04:51 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'public, max-age=60, s-maxage=60', 'Vary': 'Accept, Accept-Encoding….
You can also try this code with Online Python Compiler
Run Code

 

.headers produces a dictionary-like object with key-value access to header values. You may access Server: to examine the content type of the response payload, for example.

>>> r.headers['Server']
'GitHub.com'
You can also try this code with Online Python Compiler
Run Code

Other requests

Other prominent HTTP methods include PUT, POST, HEAD, DELETE, PATCH, and OPTIONS, in addition, to GET. For each of these HTTP methods, requests offer a method with a signature comparable to get().

An example of POST request. 

>>> requests.post('https://httpbin.org/post', data={'key':'value'})
<Response [200]>
You can also try this code with Online Python Compiler
Run Code

 

We can access the headers and content similar to what we learned in the above examples. 

>>> r.headers['server']
'gunicorn/19.9.0'

>>> r.json()['url']
'https://httpbin.org/post'
You can also try this code with Online Python Compiler
Run Code

 

POST, PUT, and the less popular PATCH requests, according to the HTTP standard, convey data through the message body rather than query string parameters. You'll provide the payload to the data argument of the associated function using requests.

A dictionary, a list of tuples, bytes, or a file-like object can be used as data. You should tailor the data you submit in the body of your request to the needs of the service with which you're engaging.

>>> requests.post('https://httpbin.org/post', data={'key':'value'})
<Response [200]>
You can also try this code with Online Python Compiler
Run Code

 

Aside from the above-mentioned methods, there are more methods such as PUT, DELETE, PATCH, OPTIONS ,and HEAD. 

>>> requests.options('https://httpbin.org/get')
>>> requests.patch('https://httpbin.org/patch', data={'key':'value'})
>>> requests.put('https://httpbin.org/put', data={'key':'value'})
>>> requests.delete('https://httpbin.org/delete')
>>> requests.head('https://httpbin.org/get')
You can also try this code with Online Python Compiler
Run Code

 

You can also practice with the help of Online Python Compiler

FAQs

  1. What is the Request library in Python?
    In Python, the requests library is the standard for sending HTTP requests. It hides the difficulties of making requests behind a beautiful, straightforward API, allowing you to concentrate on interacting with services and consuming data in your app.
     
  2. Which are the alternatives to the Request library in Python?
    Python contains two built-in modules, urllib and urllib2, to handle HTTP-related tasks. 
     
  3. What are the advantages of using the Request library?
    The Request library is very simple and easy to use. The fetching of information and invoking various types of requests such as GET, PUSH, POST, etc., is straightforward and can be achieved in fewer lines of code.

Key Takeaways

Congratulations on making it this far.

In this blog, we learned about the Request library in Python and learned its basic methods.

If you want to become proficient with Python programming, I suggest you take the Coding Ninjas Python Course, which will teach Python basics with Data Structures and Algorithms. 

Live masterclass