HTTP methods in a REST API

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

HTTP methods in a REST API

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Your app needs to fetch a list of students from a REST API without changing anything. Which HTTP method should it use?
A DELETE
B GET
2. A student submits a sign-up form to add a new user account to the server. Which method fits best?
A POST creates a new resource
B GET creates a new resource
3. Which statement about PUT is correct?
A PUT updates an existing resource
B PUT removes an existing resource
4. A user closes their account, so the app asks the server to remove their record. Which method is used?
A DELETE
B PUT
5. Which request reads the data for the student with ID 7?
A POST /students/7
B GET /students/7

Match the Pairs

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

Left columnRight column
1requests.get("https://api.school.com/students/7") APOST creates resources and does not delete them. Use DELETE to remove student 7.
2requests.delete("https://api.school.com/students/7") Brequests.put(url, json=changes) # 404
3POST creates a new resource, and the server replies 201 Created when it succeeds Crequests.post(url, json=new_student) # 201
4PUT updates an existing resource, and a 404 Not Found reply means that resource does not exist DReads (retrieves) the student with ID 7 without changing anything on the server
5requests.get(url, json=new_student) # to create a student EGET only reads data, so it cannot create a resource. Use POST to create a new student.
6requests.post(url) # to delete student 7 FRemoves the student with ID 7 from the server

Definitions

GET, POST, PUT, DELETE

Fill in the Blanks

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

1.
url = "https://api.example.com/students" r = requests.___(url) print(r.status_code)
2.
url = "https://api.example.com/students" r = requests.____(url, json=new_student)
3.
url = "https://api.example.com/students/1" r = requests.___(url, json=changes)
4.
url = "https://api.example.com/students/1" r = requests.______(url)
5.
r = requests.get(url) if r.___________ == 404: print("Student not found")

Spot the Error

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

Question 1
A # Read the list of pets
B url = "https://x.com/pets"
C r = requests.delete(url)
D print(r.json())
Question 2
A r = requests.post(url, json=d)
B if r.status_code == 404:
C print("Pet created")
Question 3
A url = "https://x.com/pets/7"
B d = {"name": "Max"}
C # Update pet 7
D r = requests.post(url, json=d)
Question 4
A url = "https://x.com/pets/7"
B r = requests.put(url)
C print(r.status_code)
Question 5
A r = requests.get(url)
B print(r.status_code)
C if r.status_code == 201:
D print(r.json())
E else:
F print("Pet not found")

Answer Key

HTTP methods in a REST API
Section A: Quick Questions
Q1
B — GET
Q2
A — POST creates a new resource
Q3
A — PUT updates an existing resource
Q4
A — DELETE
Q5
B — GET /students/7
Section B: Match the Pairs
Q1
requests.get("https://api.school.com/students/7") → Reads (retrieves) the student with ID 7 without changing anything on the server
Q2
requests.delete("https://api.school.com/students/7") → Removes the student with ID 7 from the server
Q3
POST creates a new resource, and the server replies 201 Created when it succeeds → requests.post(url, json=new_student) # 201
Q4
PUT updates an existing resource, and a 404 Not Found reply means that resource does not exist → requests.put(url, json=changes) # 404
Q5
requests.get(url, json=new_student) # to create a student → GET only reads data, so it cannot create a resource. Use POST to create a new student.
Q6
requests.post(url) # to delete student 7 → POST creates resources and does not delete them. Use DELETE to remove student 7.
Section C: Definitions
Q1
GET, POST, PUT, DELETE — HTTP methods in a REST API: GET reads a resource, POST creates one, PUT updates one and DELETE removes one. The server replies with a status code such as 200 OK or 404 Not Found.
Section D: Fill in the Blanks
Q1
url = "https://api.example.com/students" r = requests.get(url) print(r.status_code)
Q2
url = "https://api.example.com/students" r = requests.post(url, json=new_student)
Q3
url = "https://api.example.com/students/1" r = requests.put(url, json=changes)
Q4
url = "https://api.example.com/students/1" r = requests.delete(url)
Q5
r = requests.get(url) if r.status_code == 404: print("Student not found")
Section E: Spot the Error
Q1
C — The program wants to read the list of pets, so it needs GET. DELETE removes a resource. The line should be r = requests.get(url).
Q2
B — 404 means Not Found. A successful POST that creates a resource returns 201 Created, so the check should be r.status_code == 201.
Q3
D — POST creates a new resource. To update the existing pet 7 you use PUT, so the line should be r = requests.put(url, json=d).
Q4
B — The program is meant to remove pet 7. PUT updates a resource, while DELETE removes it. The line should be r = requests.delete(url).
Q5
C — 201 means Created, which is used for POST. A successful GET returns 200 OK, so the check should be r.status_code == 200.