REST architecture

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

REST architecture

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. In REST, what does a URL such as /students/12 identify?
A A resource (the student with ID 12)
B An action the server must run
2. Which statement describes a stateless REST request?
A Each request carries everything the server needs to process it
B The server remembers earlier requests from the same client
3. Which URL is better REST design for fetching one product?
A GET /getProductNumberFive
B GET /products/5
4. A client sends a second request to a REST API. What can the server assume about the first request?
A The client is the same one that asked before and wants a related answer
B Nothing, because each request stands alone
5. Which describes client-server separation in REST?
A The front-end and the server can change independently as long as the API stays the same
B The client must know how the server stores its database

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1GET /students/42 AAsks the server for the resource that has the URL /students/42, using a standard HTTP method
2GET /orders/7 Authorization: Bearer abc123 BA stateless request that carries everything the server needs, including the credentials, so the server remembers nothing from earlier requests
3Resources are identified by URLs, so each item of data gets its own address CThe server relies on remembered state from an earlier request, so the request is not stateless. The request should include the page number, such as /items?page=3.
4Client-server separation: the client asks for data and the server sends it back, with neither depending on how the other works internally Dresponse = requests.get("https://api.school.edu/students/42")
5GET /getStudent?action=fetch&id=42 Ehttps://api.school.edu/students/42/results
6GET /next-page (server uses its memory of the last page sent) FThe URL describes an action instead of a resource. REST should use a resource URL like /students/42 and let the HTTP method (GET) show the action.

Definitions

REST

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
url = "https://api.school.com/students/7" response = requests.___(url)
2.
___ /students/7 HTTP/1.1 Host: api.school.com
3.
token = "abc123" headers = {"Authorization": _____}
4.
response = requests.get(url) data = response.____()
5.
body = {"name": "Zoe"} requests.____(url, json=body)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A import requests
B url = "http://api.co/items/3"
C r = requests.fetch(url)
D print(r.status_code)
Question 2
A GET /users
B GET /users/7
C GET /users/7/orders
D GET /getOrder?id=12
Question 3
A U = "http://api.co/me"
B h = {"Authorization": "abc"}
C r = requests.get(U)
D print(r.status_code)
Question 4
A url = "http://api.co/users/7"
B d = {"house": "Blue"}
C r = requests.get(url, json=d)
D print(r.status_code)
Question 5
A import requests
B url = "http://api.co/users"
C r = requests.get(url)
D for u in r.json:
E print(u["name"])

Answer Key

REST architecture
Section A: Quick Questions
Q1
A — A resource (the student with ID 12)
Q2
A — Each request carries everything the server needs to process it
Q3
B — GET /products/5
Q4
B — Nothing, because each request stands alone
Q5
A — The front-end and the server can change independently as long as the API stays the same
Section B: Match the Pairs
Q1
GET /students/42 → Asks the server for the resource that has the URL /students/42, using a standard HTTP method
Q2
GET /orders/7 Authorization: Bearer abc123 → A stateless request that carries everything the server needs, including the credentials, so the server remembers nothing from earlier requests
Q3
Resources are identified by URLs, so each item of data gets its own address → https://api.school.edu/students/42/results
Q4
Client-server separation: the client asks for data and the server sends it back, with neither depending on how the other works internally → response = requests.get("https://api.school.edu/students/42")
Q5
GET /getStudent?action=fetch&id=42 → The URL describes an action instead of a resource. REST should use a resource URL like /students/42 and let the HTTP method (GET) show the action.
Q6
GET /next-page (server uses its memory of the last page sent) → The server relies on remembered state from an earlier request, so the request is not stateless. The request should include the page number, such as /items?page=3.
Section C: Definitions
Q1
REST — A web API style where data is treated as resources, each with its own URL, accessed using standard HTTP requests. Each request is stateless, so the server remembers nothing between requests.
Section D: Fill in the Blanks
Q1
url = "https://api.school.com/students/7" response = requests.get(url)
Q2
GET /students/7 HTTP/1.1 Host: api.school.com
Q3
token = "abc123" headers = {"Authorization": token}
Q4
response = requests.get(url) data = response.json()
Q5
body = {"name": "Zoe"} requests.post(url, json=body)
Section E: Spot the Error
Q1
C — The requests library has no fetch() function. To read a resource from a REST API you send a GET request, so the line should be r = requests.get(url).
Q2
D — REST URLs name a resource (a noun) and the HTTP method is the action. A verb like getOrder in the URL breaks this. It should be GET /orders/12.
Q3
C — REST requests are stateless, so the server does not remember who you are from an earlier request. The credentials must be sent with every request: requests.get(U, headers=h).
Q4
C — GET only reads a resource. To update an existing resource at its URL you send a PUT request: requests.put(url, json=d).
Q5
D — json is a method of the response, so it must be called with brackets. The server sends the resource as JSON and the client reads it with r.json().