401 — Making HTTP Requests with requests

Beginner

Install the requests library and use it to send GET and POST calls to real public APIs, inspect response objects, handle HTTP errors with raise_for_status(), and catch network failures with try/except. Every request in this lesson includes an explicit timeout — the single most important habit for production HTTP scripts. The lesson closes by importing load_config() from lesson 304's layered_config.py to show how api_base_url flows from the config layer directly into requests calls.

Learning Objectives

1
Install the requests library with pip and confirm the installed version
2
Send a GET request to a public API with an explicit timeout and inspect the response
3
Access response attributes: status_code, headers, text, and json()
4
Send a POST request with a JSON body using the json= parameter
5
Use raise_for_status() to automatically raise an exception on 4xx and 5xx responses
6
Catch the three categories of requests errors: Timeout, ConnectionError, and HTTPError
7
Build a safe_get() wrapper function that combines timeout, raise_for_status, and error handling
8
Import load_config() from layered_config.py (lesson 304) and use config.api_base_url to construct request URLs
Step 1

Create the lesson-401 workspace and install requests

Create the lesson-401 directory, activate the virtual environment from lesson 101, and install the requests library. Confirm the version and explore the top-level objects the package exposes.

Commands to Run

mkdir -p ~/devops-python/lesson-401
cd ~/devops-python/lesson-401
source ~/devops-python/lesson-101/devops-env/bin/activate
pip install 'requests>=2.31'
pip show requests | grep -E 'Name|Version|Location'
python3 -c "import requests; print('requests version:', requests.__version__); print('Key classes:', [x for x in dir(requests) if not x.startswith('_') and x[0].isupper()])"
python3 -c "import requests; help(requests.get)" 2>&1 | head -20

What This Does

requests is not part of the Python standard library — it must be installed from PyPI.

The >=2.31 pin avoids older versions with known CVEs. pip show confirms the installed version and where the package lives on disk.

The third command lists the public classes that requests exposes: Response, Session, Request, and the exception types (HTTPError, Timeout, ConnectionError, etc.).

These are the objects you will use in every step of this lesson.

Expected Outcome

pip show prints Name: requests and a version of 2.31 or later.

The class list includes Response, Session, HTTPError, Timeout, and ConnectionError. help(requests.get) shows the function signature; in requests >=2.32 the signature reads get(url, params=None, **kwargs: Unpack[GetKwargs]) — timeout is one of the keyword arguments accepted through **kwargs rather than listed explicitly in the signature.

Pro Tips

  • 1
    The requests library ships with `urllib3` as a dependency — you do not import urllib3 directly. requests provides a clean, intentional API on top of it.
  • 2
    Run `pip show requests` after installing to confirm the exact version. The version number matters for reproducibility: pin it in a requirements.txt (`requests==2.31.0`) for any script you deploy.

Common Mistakes to Avoid

  • āš ļøInstalling requests at the system Python level instead of inside the virtual environment — `pip install requests` without activating the venv installs it globally. Always activate the venv first (`source .../devops-env/bin/activate`) and confirm the prompt prefix shows the venv name.
  • āš ļøImporting `Request` from requests and confusing it with `requests.get()` — the `Request` class is the internal prepared-request object. For sending HTTP calls, use `requests.get()`, `requests.post()`, or a `Session` object.
Was this step helpful?

All Steps (0 / 8 completed)