generated from mwc/lab_riddles
Finished Lab N-1 Riddles Checkpoints 1-3
One internet-enabled service I use regularly is Google Classroom. I often will post an assignment, students can view the assignement, then submit their work online. I hypothesize that when I post an assignment, my computer sends an HTTP POST request to the Google Classroom server. When students view the assignment, their computers send an HTTP GET request to the server to retrieve the assignment details, which are then displayed on their screens. When students submit their work, another HTTP POST request is sent to the server with their submission data, which is then stored and associated with their account. This lab's exploration of requests and responses has helped me understand what happens behind the scenes when I use technology or any platform.I now understand that every time I interact with an app or website, there are multiple HTTP requests and responses happening.
This commit is contained in:
18
api.py
18
api.py
@@ -36,16 +36,28 @@ class RiddleAPI:
|
|||||||
def get_riddle(self, riddle_id):
|
def get_riddle(self, riddle_id):
|
||||||
"Fetches a single riddle from the server"
|
"Fetches a single riddle from the server"
|
||||||
route = "/show"
|
route = "/show"
|
||||||
raise NotImplementedError("The API doesn't support `get_riddle` yet. Can you add it?")
|
params = {'id': riddle_id}
|
||||||
|
response= requests.get(self.server_url + route, params=params)
|
||||||
|
if response.ok:
|
||||||
|
return response.json()
|
||||||
|
else:
|
||||||
|
raise APIError(response.json()['errors'])
|
||||||
|
|
||||||
def get_random_riddle(self):
|
def get_random_riddle(self):
|
||||||
"Fetches all riddles from the server and then randomly returns one"
|
"Fetches all riddles from the server and then randomly returns one"
|
||||||
raise NotImplementedError("The API doesn't support `get_random_riddle` yet. Can you add it?")
|
riddles = self.get_all_riddles()
|
||||||
|
return choice(riddles)
|
||||||
|
# raise NotImplementedError("The API doesn't support `get_random_riddle` yet. Can you add it?")
|
||||||
|
|
||||||
def add_riddle(self, question, answer):
|
def add_riddle(self, question, answer):
|
||||||
"Adds a new riddle to the server"
|
"Adds a new riddle to the server"
|
||||||
route = "/new"
|
route = "/new"
|
||||||
raise NotImplementedError("The API doesn't support `add_riddle` yet. Can you add it?")
|
params = {'question': question, 'answer': answer}
|
||||||
|
response = requests.post(self.server_url + route, json=params)
|
||||||
|
if response.ok:
|
||||||
|
return response.json()
|
||||||
|
else:
|
||||||
|
raise APIError(response.json()['errors'])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
33
notes.md
33
notes.md
@@ -7,7 +7,40 @@ about the meaning of the line, and some situation in which it might be useful.
|
|||||||
You are welcome to research the meanings of these headers, but it's also
|
You are welcome to research the meanings of these headers, but it's also
|
||||||
fine to speculate for now.
|
fine to speculate for now.
|
||||||
|
|
||||||
|
Line- GET /all HTTP/1.1
|
||||||
|
Inference: This line is telling my computer to GET or retrieve all of the data that is located at the /all path using HTTP version 1.1
|
||||||
|
Useful: This is useful because it tells the server what the user is asking for. In this case, it is retrieving all riddles from the riddles database.
|
||||||
|
|
||||||
|
Line- Host: riddles.makingwithcode.org
|
||||||
|
Inference: This line tells the server which website/domain the request is being sent to.
|
||||||
|
Useful: When multiple websites are hosted on the same server so the server knwows which one the client is trying to reach.
|
||||||
|
|
||||||
|
Line- Connection: keep-alive
|
||||||
|
Inference: This line tells the server to keep the connnection open after the current request so it can be used instead of opening a new one each time.
|
||||||
|
Useful: When multiple requests are being made, it doesn't need to reconnect every time.
|
||||||
|
|
||||||
|
Line- Content-Type: application/json
|
||||||
|
Inference: This is telling your computer that the response body is written in json format.
|
||||||
|
Useful: Now your computer knows how to interpret the data it receives.
|
||||||
|
|
||||||
|
Line- HTTP/1.1 200 OK
|
||||||
|
Inference: This means that the request was successful and the server was able to return the requested data.
|
||||||
|
Useful: That there were no errors retrieving the information.
|
||||||
|
|
||||||
|
|
||||||
## Checkpoint 2
|
## Checkpoint 2
|
||||||
The goal of this checkpoint is to see what status codes you can get back from
|
The goal of this checkpoint is to see what status codes you can get back from
|
||||||
the riddle server. Paste below several `http` requests and the status codes
|
the riddle server. Paste below several `http` requests and the status codes
|
||||||
they return.
|
they return.
|
||||||
|
|
||||||
|
http -v post https://riddles.makingwithcode.org/guess id=1 answer="a short-legged cow"
|
||||||
|
HTTP/1.1 200 OK
|
||||||
|
|
||||||
|
http -v post https://riddles.makingwithcode.org/guess id= answer="a short-legged cow"
|
||||||
|
HTTP/1.1 400 Bad Request
|
||||||
|
|
||||||
|
http -v post https://riddles.makingwithcode.org/guess id=100 answer="a short-legged cow"
|
||||||
|
HTTP/1.1 404 Not Found
|
||||||
|
|
||||||
|
http -v get https://riddles.makingwithcode.org/guess id=1 answer="a short-legged cow"
|
||||||
|
HTTP/1.1 405 Method Not Allowed
|
||||||
Reference in New Issue
Block a user