Index Ai Academy
Phase 8 · APIsHTTP from the wire up

HTTP from the wire up

About 35 minLooking ahead: some prerequisites aren't done yet

Why this matters

Every time your code talks to an API, two computers exchange a few lines of plain text. Libraries like requests hide that text, which is convenient until something breaks. A header goes missing, a message is cut short, or a web address gets changed on the way. If you can picture the raw messages, you can fix those problems in minutes. It's also where building your own server starts, in the next lessons.

  • Describe the layers under every API call: DNS, TCP, TLS and HTTP
  • Read and write a raw HTTP request and response, byte by byte
  • Split a URL into its parts and encode query strings correctly
  • Parse raw HTTP yourself, the first job of every web server

Four steps under one call

Picture posting a letter to a company you've never written to. First you look up its address. Then you need a route that gets the letter there safely. You seal it in an envelope, so nobody can read it on the way. Only then does the letter itself matter.

A call like requests.get("https://api.gradient.test/v1/campaigns") goes through the same four steps, in order:

  1. DNS is the address book. It turns the name api.gradient.test into the server's numeric address, called its IP address.
  2. TCP opens a connection to that address. A server has numbered doors called ports: 443 for HTTPS and 80 for plain HTTP. The internet only promises to try to deliver small packets of data, and some get lost or arrive out of order. TCP fixes that, so your program sees a steady stream of bytes, in the right order. Think of a phone line that never drops a word.
  3. TLS seals the envelope. It encrypts the stream, so nobody in between can read it. It also checks the server's certificate, which is proof that the server is who it claims to be; so TLS adds encryption and proof of identity. The certificate proves this really is api.gradient.test. HTTPS is HTTP inside TLS.
  4. HTTP is the letter itself: a request in a fixed text format, and a reply in the same style.

Each step only relies on the one below it. HTTP doesn't care how TCP delivers its bytes, which is why the same HTTP works over Wi-Fi, fiber or a phone network.

In plain words: find the address, open a reliable line, seal it, then send a letter in an agreed format.