Site icon StudyGyaan

Requests Python Library Cheatsheet

When it comes to making HTTP requests in Python, the requests library stands out as a powerful and user-friendly tool. Whether you’re interacting with REST APIs, fetching data from web pages, or sending data to a server, requests makes the process straightforward. In this cheatsheet, we’ll cover the essentials to help you master the art of using requests effectively.

Installation

Before diving into the cheatsheet, make sure you have the requests library installed. If you don’t have it yet, install it using:

pip install requests

Now, let’s explore the key functionalities of requests.

Making a GET Request

import requests

url = "https://api.example.com/data"
response = requests.get(url)

# Check if the request was successful (status code 200)
if response.status_code == 200:
    data = response.json()  # Parse JSON response
    print(data)
else:
    print(f"Error: {response.status_code}")

Making a POST Request

import requests

url = "https://api.example.com/create"
payload = {"key1": "value1", "key2": "value2"}

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

if response.status_code == 201:  # Created
    print("Resource created successfully.")
else:
    print(f"Error: {response.status_code}")

Passing URL Parameters

import requests

url = "https://api.example.com/search"
params = {"query": "python", "type": "programming"}

response = requests.get(url, params=params)

if response.status_code == 200:
    results = response.json()
    print(results)
else:
    print(f"Error: {response.status_code}")

Handling Headers

import requests

url = "https://api.example.com/endpoint"
headers = {"Authorization": "Bearer YOUR_ACCESS_TOKEN"}

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

if response.status_code == 200:
    data = response.json()
    print(data)
else:
    print(f"Error: {response.status_code}")

Uploading Files

import requests

url = "https://api.example.com/upload"
files = {"file": open("example.txt", "rb")}

response = requests.post(url, files=files)

if response.status_code == 200:
    print("File uploaded successfully.")
else:
    print(f"Error: {response.status_code}")

Handling Timeouts

import requests

url = "https://api.example.com/timeout"
timeout_seconds = 5

try:
    response = requests.get(url, timeout=timeout_seconds)
    response.raise_for_status()  # Raise an exception for HTTP errors
    print(response.text)
except requests.exceptions.Timeout:
    print("Request timed out.")
except requests.exceptions.RequestException as e:
    print(f"Error: {e}")

Session Management

import requests

# Create a session for making multiple requests with shared parameters (e.g., cookies)
session = requests.Session()

# Perform multiple requests with the same session
response1 = session.get("https://api.example.com/login", params={"user": "john", "password": "secret"})
response2 = session.get("https://api.example.com/dashboard")

# Close the session when done
session.close()

This requests cheatsheet provides a quick reference for common scenarios when working with HTTP requests in Python. Whether you’re fetching data, sending parameters, or handling file uploads, requests simplifies the process, making it an invaluable tool for web developers and data scientists alike. Experiment with these examples and explore the extensive documentation to uncover even more features and capabilities.

FAQ

1. What is the requests library in Python?

The requests library is a popular Python module used for making HTTP requests. It simplifies the process of sending HTTP requests and handling responses, making it an essential tool for tasks such as interacting with APIs, fetching data from web pages, or submitting form data.

2. How can I handle authentication with requests?

requests supports various authentication methods. For basic authentication, you can include the auth parameter in your request. For example, if using OAuth, you can pass the access token through headers using the Authorization field. Always follow the authentication method specified by the API or service you are interacting with.

3. How do I handle errors and exceptions with requests?

To handle errors with requests, check the HTTP status code of the response using response.status_code. A status code in the 4xx or 5xx range typically indicates an error. You can also use response.raise_for_status() to raise an exception for HTTP errors. Additionally, use try-except blocks to catch and handle exceptions like timeouts or connection errors.

4. What’s the difference between get() and post() methods in requests?

The get() method is used to retrieve data from a specified URL, while the post() method is used to send data to a specified URL. Typically, you use get() for retrieving information, and post() for actions that modify data on the server, such as submitting forms or creating resources.

5. How can I pass parameters in a URL with requests?

You can pass parameters in a URL using the params parameter in the get() method. Simply create a dictionary with the key-value pairs of your parameters and pass it as an argument. requests will automatically append the parameters to the URL. For example:
import requests url = "https://api.example.com/data" params = {"param1": "value1", "param2": "value2"} response = requests.get(url, params=params)
This is useful for API queries or any scenario where you need to include additional information in the URL.

Exit mobile version