Mastering HTTP Requests with Python's Requests Module: Practical Examples

The Python Requests library is a popular choice for working with HTTP requests. It's user-friendly, well-documented, and offers a wide range of features. In this blog post, we'll explore practical examples of using the Requests module to send various types of HTTP requests, handle redirects, and more.

Installation

To install the Requests library, simply run the following command:

pip install requests

Sending GET Requests

A GET request is the most basic type of HTTP request, used for fetching data from a server. Let's start by sending a GET request to a sample API:

import requests

url = 'https://jsonplaceholder.typicode.com/todos/1'
response = requests.get(url)

print(response.text)

Handling JSON Responses

Many APIs return data in JSON format. The Requests library makes it easy to parse JSON responses:

import requests

url = 'https://jsonplaceholder.typicode.com/todos/1'
response = requests.get(url)

data = response.json()
print(data)

Sending POST Requests

POST requests are used to submit data to a server, typically to create a new resource. Here's an example of sending a POST request:

import requests

url = 'https://jsonplaceholder.typicode.com/posts'
data = {
    'title': 'New post',
    'body': 'This is the content of my new post.',
    'userId': 1
}

response = requests.post(url, json=data)

print(response.status_code)
print(response.json())

Sending PUT and DELETE Requests

PUT and DELETE requests are used to update and delete resources, respectively. Here are examples of both request types:

import requests

# PUT request
url = 'https://jsonplaceholder.typicode.com/posts/1'
data = {
    'title': 'Updated post',
    'body': 'This is the updated content.',
    'userId': 1
}

response = requests.put(url, json=data)
print(response.status_code)

# DELETE request
url = 'https://jsonplaceholder.typicode.com/posts/1'
response = requests.delete(url)
print(response.status_code)

Handling Redirects

By default, Requests follows redirects automatically. However, you can disable or customize this behavior using the allow_redirects parameter:

import requests

url = 'http://httpbin.org/redirect/1'
response = requests.get(url, allow_redirects=False)

print(response.status_code)
print(response.headers.get('Location'))

Customizing Headers

You can also customize headers for your requests. This is useful for setting authentication tokens, modifying the user agent, and more:

import requests

url = 'https://jsonplaceholder.typicode.com/todos/1'
headers = {
    'Authorization': 'Bearer your_token_here',
    'User-Agent': 'MyApp/1.0'
}

response = requests.get(url, headers=headers)
print(response.text)

Handling Timeouts

Finally, it's essential to handle timeouts in your requests to prevent your application from hanging indefinitely. You can set the timeout using the timeout parameter:

import requests

url = 'https://jsonplaceholder.typicode.com/todos/1'

try:
    response = requests.get(url, timeout=5)
    print(response.text)
except requests.exceptions.Timeout:
    print('Request timed out')

In conclusion, the Python Requests library offers a simple yet powerful way to work with HTTP requests. With a wide range of features and a user-friendly API, it's an invaluable tool for any Python developer working with APIs and web services.

An AI coworker, not just a copilot

View VelocityAI