Making HTTP requests
Modern Python programs often need to talk to other systems. That usually happens over HTTP, using simple requests to fetch or send data. In AI-capable software, this is how we reach APIs, services, and data sources that live outside our own codebase.
This lesson introduces the basic shape of an HTTP request in Python, using a widely adopted third-party library. The goal is to recognize what an HTTP request is, why we use a helper library, and what a minimal request looks like in practice.
What an HTTP request is
An HTTP request is a structured message sent from one program to another over the web. It asks for something, such as a web page, a document, or a piece of data.
At a high level, a request includes a method (such as GET), a target address (a URL), and optional extra information. We do not need to know the low-level protocol details to use HTTP effectively in Python.
For now, it is enough to recognize that an HTTP request is how our program says, “Please give me this resource.”
Installing and importing the requests library
Python’s standard library includes basic HTTP tools, but most real programs use a higher-level library. The most common choice is requests, which provides a clean and readable interface.
If requests is not already installed, we install it using pip from the command line.
pip install requests
Once installed, the library is imported like any other module.
import requests
From this point on, we can create HTTP requests using simple function calls.
Making a simple HTTP GET request
A GET request asks for data from a given URL. With requests, this is a single function call.
import requests
response = requests.get("https://example.com")
The call returns a response object. This object represents everything the server sent back, including headers, status information, and the response body.
At this stage, we are focused only on making the request successfully.
Supplying query parameters in a request
Many URLs accept query parameters. These are name–value pairs added to the request to refine what data is returned.
With requests, query parameters are supplied as a dictionary.
import requests
params = {
"category": "planets",
"format": "text"
}
response = requests.get("https://example.com/data", params=params)
The library takes care of encoding these parameters into the URL. This keeps our code readable and avoids manual string manipulation.
Reading the response body as text
Once we have a response, we usually want the data it contains. When the response is textual, it can be accessed as a string.
import requests
response = requests.get("https://example.com")
html = response.text
The text attribute gives us the response body decoded as text. From here, it can be printed, saved to a file, or passed to other parts of the program.
This pattern—make a request, receive a response, read the body—is the foundation of most API interactions.
Conclusion
We now have a clear picture of what an HTTP request looks like in Python and why the requests library is used so often. We can install the library, make a basic GET request, supply query parameters, and read the response body as text.
That orientation is enough to recognize and reason about HTTP calls in real Python programs, and to build on this foundation when working with APIs and external services.