Riddles
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:
- The domain name system, which translates names like
makingwithcode.org
into IP addresses like13.33.165.44
- TCP/IP, the process of packaging messages, routing them across the internet to their destination, and checking that they arrived intact.
- The physical networks of wires (or wifi/satellite links) connecting the computers to each other.
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
2 GET /all HTTP/1.1
3 Accept: */*
4 Accept-Encoding: gzip, deflate
5 Connection: keep-alive
6 Host: riddles.makingwithcode.org
7 User-Agent: HTTPie/3.2.1
8
9
10
11 HTTP/1.1 200 OK
12 Connection: keep-alive
13 Content-Length: 116
14 Content-Type: application/json
15 Cross-Origin-Opener-Policy: same-origin
16 Date: Tue, 05 Mar 2024 00:25:26 GMT
17 Referrer-Policy: same-origin
18 Server: nginx/1.22.0 (Ubuntu)
19 X-Content-Type-Options: nosniff
20 X-Frame-Options: DENY
21
22 {
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 ]
32 }
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.
GET
is used when you want to fetch data from the server without changing anything.POST
is used when you're making a change. For example, logging in, placing an order, unfriending someone on social media.
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:
- Codes in the 200s mean the request succeeded.
- Codes in the 300s mean you need to do something else--often the page has moved and you need to go to the new URL.
- Codes in the 400s mean you did something wrong: you asked for something that doesn't exist, you used the wrong verb, you didn't provide the required information, you don't have permission to see this, etc.
- Codes in the 500s mean something went wrong on the server.
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:
- Carefully read the RiddleAPI methods which have been implemented:
get_all_riddles
andguess_riddle
. Use these as models for the others. - RiddleAPI uses a library called
requests
to send HTTP requests. Userequests.get()
forGET
requests andrequests.post()
forPOST
requests. - The result of
requests.get()
orrequests.post()
is aresponse
object.response.ok
is a boolean (True/False) indicating whether the request succeeded (whether its status code is in the 200s).response.json()
returns the data structure sent in the response.