1. Concept library
  2. Python Foundations
  3. Module 10: Working with APIs

Concept

HTTP requests

Programs talk to web services by sending HTTP requests and reading the response: a status code, headers and a body. The requests library makes this a few lines.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain what a request sends and what a response contains
  2. Write a GET request with requests, including a timeout, and read the status and body
  3. Pass query parameters and headers without building the URL by hand

In practice

A few lines that show HTTP requests doing its job.

Python
import requests

response = requests.get(
    "https://api.github.com/repos/python/cpython",
    params={"per_page": 1},
    timeout=10,
)
print(response.status_code)           # 200
print(response.headers["content-type"])
print(response.json()["stargazers_count"])

The common mistake

Watch for

Calling requests.get() without a timeout. The default is to wait forever, so one slow server can hang your whole program.

What to know first

Your roadmap puts these before HTTP requests, unless you already know them.

How it stays learned

Once a session shows you understand HTTP requests, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on HTTP requests.

Next in the moduleJSON data →