Making GET Requests and Handling Responses

Making GET Requests and Handling Responses

Welcome to the second lesson in our journey of interacting with APIs in Python. In the previous lesson, we laid a strong foundation by understanding RESTful APIs and how HTTP requests facilitate interactions with them. We used curl to manually craft and send a GET request to an endpoint. Now, we are transitioning to automating this process using Python. This lesson introduces the requests library, which allows us to send HTTP requests effortlessly in our scripts, making it an invaluable tool in the realm of web development and API integration.

Setting Up the Environment

To make HTTP requests in Python, we use the requests library, a user-friendly and powerful library for handling HTTP requests. If you're working within the CodeSignal environment, this library is conveniently pre-installed. However, to work with it on your personal device, you'll can install it by running the following pip command in your terminal or command prompt.

pip install requests

Once installed, you can import the requests library in your Python code, making its functionalities available for sending HTTP requests in our script.

import requests

With this setup, you'll be equipped to automate requests, saving time and boosting efficiency in your development workflow.

Defining the Base URL

When interacting with an API, it's helpful to define a base URL for the service you're communicating with. This makes your code more modular and easy to maintain, allowing you to change the root of your API URL in one place without modifying each request.

# Base URL for the API
base_url = "http://localhost:8000"

By setting this base URL, we can easily concatenate endpoints for different services within the API, making our code cleaner and more adaptable.

Performing a Basic GET Request

Let's dive into the process of fetching data from an API using Python's requests library. Our goal is to retrieve a list of to-do items from the /todos endpoint using the GET method.

# Fetch all todos using the get method
response = requests.get(f"{base_url}/todos")

# Print raw response
print("Raw Response:")
print(response.text)

By using the requests.get() method, we send a GET request to the constructed full URL. The response from the server, stored in the variable response, is then printed using response.text, which gives us the raw response body as a string.

Here’s an example of what the raw response might look like:

Raw Response:
[
  {
    "description": "Milk, eggs, bread, and coffee",
    "done": false,
    "id": 1,
    "title": "Buy groceries"
  },
  {
    "description": "Check in and catch up",
    "done": true,
    "id": 2,
    "title": "Call mom"
  },
  {
    "description": "Summarize Q4 performance metrics",
    "done": false,
    "id": 3,
    "title": "Finish project report"
  },
  {
    "description": "30 minutes of cardio",
    "done": true,
    "id": 4,
    "title": "Workout"
  }
]

This raw output allows us to see the immediate result returned by the server, serving as a starting point for further processing of the data.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal