September 11, 2024

Python requests Module – HTTP Requests

The requests module in Python is a popular library for making HTTP requests. It provides an easy-to-use interface for sending HTTP/1.1 requests, handling responses, and managing various HTTP methods. The requests module simplifies the process of interacting with web services and APIs.

1. Installation

To use the requests module, you need to install it first. You can install it using pip:

pip install requests

2. Making Basic HTTP Requests

The requests module supports various HTTP methods, including GET, POST, PUT, DELETE, and more. Here’s how to perform basic HTTP requests:

GET Request

import requests

# Send a GET request
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')

# Print the status code and response content
print(response.status_code)  # Output: 200
print(response.json())       # Output: JSON response
    

The requests.get() function sends a GET request to the specified URL and returns a response object. You can access the status code and response content using the status_code and json() methods, respectively.

POST Request

import requests

# Data to be sent in the POST request
data = {'title': 'foo', 'body': 'bar', 'userId': 1}

# Send a POST request
response = requests.post('https://jsonplaceholder.typicode.com/posts', json=data)

# Print the status code and response content
print(response.status_code)  # Output: 201
print(response.json())       # Output: JSON response with created data
    

The requests.post() function sends a POST request to the specified URL with the provided data. The json parameter automatically serializes the data to JSON format.

PUT Request

import requests

# Data to be updated
data = {'id': 1, 'title': 'foo', 'body': 'bar', 'userId': 1}

# Send a PUT request
response = requests.put('https://jsonplaceholder.typicode.com/posts/1', json=data)

# Print the status code and response content
print(response.status_code)  # Output: 200
print(response.json())       # Output: JSON response with updated data
    

The requests.put() function sends a PUT request to update the specified resource with the provided data.

DELETE Request

import requests

# Send a DELETE request
response = requests.delete('https://jsonplaceholder.typicode.com/posts/1')

# Print the status code
print(response.status_code)  # Output: 200
    

The requests.delete() function sends a DELETE request to delete the specified resource.

3. Handling Query Parameters

You can include query parameters in your requests using the params parameter:

import requests

# Query parameters
params = {'userId': 1}

# Send a GET request with query parameters
response = requests.get('https://jsonplaceholder.typicode.com/posts', params=params)

# Print the status code and response content
print(response.status_code)  # Output: 200
print(response.json())       # Output: JSON response with filtered data
    

4. Handling Headers

You can include custom headers in your requests using the headers parameter:

import requests

# Custom headers
headers = {'Authorization': 'Bearer YOUR_TOKEN'}

# Send a GET request with custom headers
response = requests.get('https://api.example.com/data', headers=headers)

# Print the status code and response content
print(response.status_code)  # Output: 200
print(response.json())       # Output: JSON response
    

5. Handling Timeouts

You can specify a timeout for your requests to prevent them from hanging indefinitely:

import requests

# Send a GET request with a timeout
try:
    response = requests.get('https://jsonplaceholder.typicode.com/posts', timeout=5)
    print(response.status_code)  # Output: 200
    print(response.json())       # Output: JSON response
except requests.exceptions.Timeout:
    print("The request timed out")
    

6. Conclusion

The requests module is a versatile and easy-to-use library for making HTTP requests in Python. It simplifies the process of sending various types of requests, handling responses, and managing parameters and headers. With its user-friendly API, the requests module is a popular choice for interacting with web services and APIs.