In this lab, we are going to explore how computers talk with each other. Every time you open a webpage in a web browser or use an app on your phone, your device communicates with one or more remote computers--asking for resources like web pages and images and fetching updates. The web page or app is probably also doing some unwelcome communication: loading advertisements and sending away a record of your behavior.

If you think about it, it's incredible that you can request information from any of the billions of computers on the Internet, and get a response almost instantly. In this lab, we are going to work with HTTP (HyperText Transfer Protocol), a protocol computers use to make requests and receive responses from other computers.

HTTP relies on a bunch of other fascinating technologies which we won't explore in this lab, including:

Requests and responses

Applications usually make HTTP requests for you, but you can make also make requests manually. http is a lovely program for making http requests.

💻 Run http -v get https://riddles.makingwithcode.org/all You should see the following:

1$ http -v get https://riddles.makingwithcode.org/all
2GET /all HTTP/1.1
3Accept: */*
4Accept-Encoding: gzip, deflate
5Connection: keep-alive
6Host: riddles.makingwithcode.org
7User-Agent: HTTPie/3.2.1
11HTTP/1.1 200 OK
12Connection: keep-alive
13Content-Length: 116
14Content-Type: application/json
15Cross-Origin-Opener-Policy: same-origin
16Date: Tue, 05 Mar 2024 00:25:26 GMT
17Referrer-Policy: same-origin
18Server: nginx/1.22.0 (Ubuntu)
19X-Content-Type-Options: nosniff
20X-Frame-Options: DENY
23 "riddles": [
24 {
25 "correct": 1,
26 "difficulty": 0.6,
27 "guesses": 4,
28 "id": 1,
29 "question": "Where do you get dragon milk?"
30 }
31 ]

HTTP Verbs

Every HTTP request uses a verb (also called a method). GET and POST are by far the most common, though there are others.

The riddle server has several additional endpoints; each requires either GET or POST. Give them a try:

Guessing a riddle

To guess the answer to a riddle, send a POST request to /guess, with the id of the riddle you're guessing and your answer. The response will tell you if you are correct. The riddle server keeps statistics on guesses and uses them to calculate riddle difficulty.

http -v post https://riddles.makingwithcode.org/guess id=1 answer="a short-legged cow"

Adding a new riddle

To add a new riddle, send a POST request to /new, with question and answer.

http -v post https://riddles.makingwithcode.org/new question="What have I got in my pocket?" answer="My precious"

After you add a new riddle, get the list of all the riddles and confirm that yours has been added.

Status Codes

So far, every response you have received from the server should have started with HTTP/1.1 200 OK. 200 is a status code, indicating how things went. There are lots of status codes, but in general:

Riddle client

The riddle server is a great way to share riddles with friends, but typing out each request will get tiresome quickly. Instead, let's use a client program which makes HTTP requests for us. client.py contains a program which lets us interact with the riddle server from the command line. api.py defines RiddleAPI, or an application programming interface for the riddle server.

Run the client program. You won't get too far before it crashes with a NotImplementedError: some of the methods of RiddleAPI are missing. Your job is to implement them. A few tips: