Back to Blog
Lesson 27 of the Python: Programming from Zero with Python course
PythonAugust 13, 20263 min read

Introduction to HTTP Requests: Using the Python Requests Library

Learn how to use the requests library to fetch data from APIs. Master performing GET requests and parsing response data in this practical Python guide.

pythonapihttprequestsbackendweb-development
Detailed image of computer source code displayed on a screen, showcasing web development elements.

Previously in this course, we covered Using Third-Party Libraries to install and manage external packages. Now that you know how to bring outside code into your environment, we’ll move beyond your local machine to fetch live data from the web.

In modern backend development, your application rarely lives in a vacuum. You’ll frequently need to communicate with other services to pull in data like weather updates, stock prices, or user profiles. This communication happens over HTTP, and in Python, the industry standard for handling these interactions is the requests library.

Understanding the HTTP GET Request

When you type a URL into your browser, you are performing an HTTP GET request. You are essentially asking a server to "get" the resource located at that address and send it back to you.

When writing backend services, we use the requests library to automate this process. Instead of manually handling complex network sockets, requests gives us a clean, human-readable interface to talk to any API.

ConceptDescription
HTTPThe protocol used to transmit data over the web.
GETThe specific method used to request data from a server.
APIAn interface that allows your program to talk to another system.
requestsThe Python library that simplifies making these calls.

Installing and Using the Requests Library

Before we write any code, ensure you have the library installed in your Virtual Environments using pip install requests.

Once installed, fetching data is straightforward. Let's look at a concrete example using the JSONPlaceholder API, a free service for testing web requests.

PYTHON
import requests

# 1. Define the URL
url = "https://jsonplaceholder.typicode.com/posts/1"

# 2. Perform the GET request
response = requests.get(url)

# 3. Check if the request was successful
if response.status_code == 200:
    # 4. Parse the response body as JSON
    data = response.json()
    print(f"Title: {data[CE9178">'title']}")
else:
    print(f"Failed to retrieve data. Status code: {response.status_code}")

In this snippet:

  • requests.get(url) sends the request and returns a Response object.
  • response.status_code tells us if the server accepted our request (200 means success).
  • response.json() automatically converts the raw string response into a Python dictionary, which we can then access using keys, just like the Dictionaries for Data Mapping we covered earlier.

Hands-on Exercise

To practice, try modifying the code above to fetch data from https://jsonplaceholder.typicode.com/users/1.

Once you have the data, try to extract and print the user's name and email fields using f-strings. This will verify that you can successfully navigate the dictionary structure returned by the API.

Common Pitfalls

  1. Forgetting to Check Status Codes: Always check response.status_code or use response.raise_for_status(). A request might fail because the server is down or the URL is wrong; assuming it always succeeds will lead to confusing crashes.
  2. Assuming JSON Format: Not every URL returns JSON. If you try to call .json() on a response that is just an HTML page, your program will raise a JSONDecodeError.
  3. Blocking Execution: Requests are "synchronous," meaning your program pauses until the server replies. If the server is slow, your script will hang. We will discuss how to manage this as your project grows.

Recap

We've bridged the gap between your local script and the internet. By using the requests library, you can now perform GET requests to retrieve remote data and turn that data into usable Python dictionaries. This is the first step toward building more complex applications that integrate real-world data sources.

Up next, we will refine our skills by diving into Parsing API Responses, where we’ll handle more complex data structures and robust error handling.

Similar Posts