Python ModulesRequests TutorialIntroduction to Requests Module

Introduction to Requests

The Requests library is one of the most popular Python libraries for handling HTTP requests. It provides a simple, user-friendly way to interact with web APIs and retrieve or send data over the internet.


What is the Requests Library?

The Requests library abstracts the complexities of handling HTTP requests and responses. It allows developers to:

  • Make HTTP requests with ease.
  • Handle data in various formats (e.g., JSON, form data).
  • Manage authentication, sessions, and cookies.

Key Features of Requests

  1. Simple API: The library provides intuitive methods for making HTTP requests.
  2. Supports All HTTP Methods: Includes GET, POST, PUT, DELETE, PATCH, etc.
  3. Handles Redirects Automatically: Follows redirects by default.
  4. Easy Authentication: Supports basic and token-based authentication.
  5. Session Handling: Manages cookies and session persistence.
  6. Streaming Support: Handles large downloads efficiently.
  7. Customizable Headers: Allows you to modify request headers.

Installing the Requests Library

Before using the library, ensure it is installed in your Python environment. You can install it using pip:

Installation in Different Environments

For Local Development (IDLE, VS Code, etc.)

pip install requests

For Jupyter Notebook

!pip install requests

For Google Colab

Google Colab comes pre-installed with Requests. No additional installation is required.


A Simple Example: Making an HTTP GET Request

Let’s retrieve data from a website using the GET method:

import requests
 
# URL to fetch data from
url = "https://jsonplaceholder.typicode.com/posts/1"
 
# Send GET request
response = requests.get(url)
 
# Print status code
print("Status Code:", response.status_code)
 
# Print response content
print("Response Content:", response.text)

Output

Status Code: 200
Response Content: {
  "userId": 1,
  "id": 1,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit\n..."
}

Explanation of the Code

  1. Importing Requests: The requests library is imported to handle HTTP requests.

  2. Defining the URL: We use an example API (https://jsonplaceholder.typicode.com) for demonstration.

  3. Sending a Request: The get() method sends a GET request to the specified URL.

  4. Accessing the Response:

    • response.status_code: Provides the HTTP status code (e.g., 200 for success).
    • response.text: Retrieves the content of the response.

Try It Yourself

Problem: Fetch User Details

Write a script to retrieve user details from https://jsonplaceholder.typicode.com/users/1 and print the response content.

Show Solution
import requests
 
# URL to fetch user details
url = "https://jsonplaceholder.typicode.com/users/1"
 
# Send GET request
response = requests.get(url)
 
# Print response content
print("Response:", response.json())

The Requests library simplifies HTTP requests in Python, making it a go-to choice for developers working with web data. Start experimenting to unlock its full potential!