Chapter 16: Computers Talking to Computers (APIs & Networking)
Learning Objectives
- Understand the Client-Server model.
- Learn the anatomy of an HTTP Request and Response.
- Understand REST APIs and JSON payloads.
- Learn how your local code communicates with computers across the world.
Prerequisites
Chapter 7: Hash Maps (JSON is basically a universal hash map).
Why Does This Exist?
Everything we have built so far lived entirely on your own laptop.
But what if you are building a Weather App? Your laptop doesn't have a thermometer in London. You need data from a weather station.
What if you want to process credit cards? You cannot legally write a payment processing engine. You need to securely ask Stripe to do it for you.
We needed a universal, standardized language for two completely different computers (maybe one running Python on Mac, the other running Java on Linux) to pass data back and forth instantly over cables under the ocean.
We needed APIs (Application Programming Interfaces).
History
In the early days of the ARPANET, computers communicated using highly specific, complex, and proprietary binary protocols.
In 1989, Tim Berners-Lee invented HTTP (Hypertext Transfer Protocol) for the World Wide Web. It was simple, text-based, and human-readable.
Later, Roy Fielding defined REST (Representational State Transfer). It standardized how we use HTTP verbs (GET, POST, PUT, DELETE) to interact with data on remote servers, mapping perfectly to the CRUD database operations we learned in Chapter 15.
Mental Model
Think of a restaurant.
You are the Client (the web browser or mobile app). You sit at the table. You want a burger, but you are not allowed to go into the kitchen.
The Kitchen is the Server (the database and backend code). It has all the food and does all the work.
The Waiter is the API. You give the waiter a formatted Request ("I would like Burger #5"). The waiter walks to the kitchen, gives the order, waits, and brings back a Response (the burger on a plate).
You don't need to know how to cook a burger; you just need to know how to ask the waiter properly.
Internal Working
When you type https://api.github.com/users/alice, the OS performs a DNS lookup to convert that URL into an IP Address (e.g., 140.82.112.4).
Your computer constructs a plain-text HTTP Request:
GET /users/alice HTTP/1.1
Host: api.github.com
Accept: application/json
This text is converted to electrical pulses, routed through your modem, across fiber optic lines, to a server rack in a GitHub data center.
The server reads the text, queries its database, constructs a JSON string containing Alice's data, and sends a Response back along with a Status Code (e.g., 200 OK).
Syntax
In modern programming, we don't write the raw HTTP text ourselves. We use libraries. In Python, the requests library is the standard.
1import requests
2
3# Make a GET request (Ask the waiter for the menu)
4response = requests.get("https://pokeapi.co/api/v2/pokemon/pikachu")
5
6if response.status_code == 200:
7 data = response.json() # Converts JSON text into a Python Dictionary
8 print(data["weight"])
9else:
10 print("Error finding Pokemon.")
Visual Explanation
Tiny Example
Sending data TO a server (POST request).
1payload = {"title": "My new post", "body": "Hello World!"}
2response = requests.post("https://jsonplaceholder.typicode.com/posts", json=payload)
3print(response.status_code) # Output: 201 (Created)
Common Mistakes
Blocking the Main Thread
Why it fails: The network is incredibly slow compared to a CPU. If you make a requests.get() call, your program freezes and waits for the server to reply. If the server takes 10 seconds, your entire app is frozen for 10 seconds. In a mobile app, this causes the screen to lock up, and the OS will kill the app.
The Fix: Network requests must eventually be handled Asynchronously (running in the background while the UI keeps updating). We will cover this in Concurrency (Chapter 17).
Debugging
When APIs fail, the server tells you exactly why using a 3-digit HTTP Status Code.
200s (Success):Everything is great.201means "Created successfully".400s (Client Error):YOU messed up.400 Bad Request(missing data),401 Unauthorized(bad password),404 Not Found(typo in the URL).500s (Server Error):THEY messed up.500 Internal Server Error(their code crashed). You can't fix this; you just have to wait for them to fix their server.
Mini Project
Time: 20 minutes.
Goal: The Chuck Norris Generator.
Install the requests library. Make a GET request to https://api.chucknorris.io/jokes/random. Parse the JSON response into a dictionary. Print the value of the "value" key to display a random joke to the terminal.
Bigger Project
Time: 1.5 hours.
Goal: The Weather Dashboard.
Sign up for a free API key at OpenWeatherMap. Write a script that asks the user for a City name using input(). Construct a URL string combining the base URL, the city name, and your API key. Make the GET request. Extract the current temperature, humidity, and weather description from the nested JSON dictionary and print a nicely formatted weather report.
Production Usage
APIs are the economy of the modern internet.
When you order an Uber, the Uber app (Client) makes a POST request to Uber's API. Uber's servers make a GET request to Google Maps' API to calculate the route. Uber then makes a POST request to Stripe's API to charge your card. Stripe makes a request to Visa's API. Dozens of servers communicate silently in milliseconds.
Best Practices
- Authentication: APIs that modify data or cost money require an API Key or Token. You send this token in the "Headers" of your HTTP request. Keep these tokens secret (refer back to Chapter 14: Never commit secrets!).
- Rate Limiting: Don't put an API call inside a loop that runs 1,000 times a second. The server will detect a DDoS attack and permanently ban your IP address. Respect rate limits (e.g., max 60 requests per minute).
Interview Questions
Easy: What is JSON?
Answer: JavaScript Object Notation. It is a lightweight, human-readable text format used to transmit data structures (like arrays and dictionaries) over the network.
Medium: Explain the difference between a GET and a POST request.
Answer: GET is used to retrieve data and should have no side effects (it doesn't modify the database). POST is used to submit new data to the server to create a record, and includes a "payload" or body of data.
Hard: What makes an API "RESTful"?
Answer: It adheres to REST architectural constraints, mainly being "Stateless" (the server doesn't remember previous requests; every request must contain all necessary authentication and context) and using standard HTTP verbs mapped to resources (URLs acting as nouns, like /users/1).
Revision Sheet
- Client/Server: The browser requests, the backend serves.
- HTTP: The text-based protocol carrying the messages.
- REST: The standard mapping of GET/POST/PUT/DELETE to CRUD operations.
- JSON: The universal text format for nested data (Dictionaries/Arrays).
- Status Codes: 200 (OK), 404 (Not Found), 500 (Server Crash).
Connections
- Previous Chapter: The Server takes your API Request, converts it to SQL, runs it against the Database (Chapter 15), packages the result as JSON, and sends it back.
- Future Chapters: Waiting for the network is slow. How do we keep our program running while we wait for the server to reply? Concurrency (Chapter 17).