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.

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.
| Concept | Description |
|---|---|
| HTTP | The protocol used to transmit data over the web. |
| GET | The specific method used to request data from a server. |
| API | An interface that allows your program to talk to another system. |
| requests | The 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.
PYTHONimport 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 aResponseobject.response.status_codetells 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
- Forgetting to Check Status Codes: Always check
response.status_codeor useresponse.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. - 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 aJSONDecodeError. - 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.
Work with me

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

Next.js Full-Stack Web App Development
A fast, SEO-ready full-stack web app built with Next.js 16 — from idea to deployed product, by an engineer who ships to production.


