Python > Web Development with Python > Working with APIs > Making HTTP Requests with `requests` library`

Fetching Data from a REST API with Requests

This snippet demonstrates how to make a GET request to a REST API using the requests library in Python. It covers basic error handling and JSON parsing.

Code Snippet

This code first imports the requests library. It then defines the API endpoint to fetch data from. The try...except block is used for error handling. requests.get() makes the GET request. response.raise_for_status() checks if the request was successful (status code 200-399) and raises an HTTPError if not. response.json() parses the JSON response into a Python dictionary. Finally, the code prints specific data from the parsed JSON.

import requests

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

try:
    response = requests.get(api_url)
    response.raise_for_status()  # Raise HTTPError for bad responses (4xx or 5xx)

    data = response.json()
    print(f"Title: {data['title']}")
    print(f"Completed: {data['completed']}")

except requests.exceptions.RequestException as e:
    print(f"An error occurred: {e}")

Concepts Behind the Snippet

This snippet uses the requests library, a popular Python library for making HTTP requests. REST APIs are commonly used to transfer data over the internet using standard HTTP methods like GET, POST, PUT, and DELETE. The JSON (JavaScript Object Notation) format is widely used for representing structured data.

Real-Life Use Case

Imagine you're building a weather application. You could use this code structure to fetch weather data from a weather API like OpenWeatherMap. You'd replace the example URL with the weather API endpoint, and then parse the JSON response to display the current temperature, humidity, and other weather information in your application.

Best Practices

Always handle potential errors using try...except blocks. Use response.raise_for_status() to check for HTTP errors. Consider using environment variables to store sensitive information like API keys. Implement proper logging for debugging purposes.

Interview Tip

Be prepared to explain the difference between GET and POST requests, the meaning of HTTP status codes (e.g., 200, 400, 500), and the purpose of using the requests library over Python's built-in urllib (requests is generally easier to use).

When to Use Them

Use this code when you need to retrieve data from a REST API in your Python application. This is common for integrating with third-party services, fetching data for web applications, and building data pipelines.

Memory Footprint

The memory footprint is relatively small for simple GET requests. However, if you are downloading large files, consider using streaming to avoid loading the entire file into memory at once.

Alternatives

Alternatives to the requests library include urllib (Python's built-in HTTP client library) and aiohttp (for asynchronous HTTP requests). httpx is also another popular choice.

Pros

requests is easy to use, well-documented, and widely adopted. It supports various features like authentication, sessions, and SSL verification.

Cons

requests is synchronous, meaning it will block until the request is complete. For highly concurrent applications, consider using an asynchronous library like aiohttp.

FAQ

  • What does response.raise_for_status() do?

    It checks if the HTTP response status code indicates an error (4xx or 5xx). If it does, it raises an HTTPError exception. This allows you to easily handle unsuccessful requests.
  • How can I send data with a POST request?

    Use the requests.post() method. You can pass data in the data parameter (for form-encoded data) or the json parameter (for JSON data). For example: response = requests.post(api_url, json={'key': 'value'}).