Calling a web API with requests.get()

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

Calling a web API with requests.get()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line of code sends a request to a web API?
A response = requests.get(url)
B response = requests.send(url)
2. What must you write at the top of your program before using requests.get()?
A import requests
B import request.get
3. What does requests.get(url) return?
A Only the plain text of the web page
B A response object
4. Which status code means the API request was successful?
A 200
B 404
5. Which line checks whether the request succeeded?
A if requests.status_code == 200:
B if response.status_code == 200:

Match the Pairs

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

Left columnRight column
1import requests ALoads the third-party requests library so the program can send web requests
2response = requests.get(url) Bstatus_code is an integer, so comparing it to the string "200" is never true. It should be compared to the number 200.
3Pass extra parameters to an API by giving requests.get() a params dictionary CSends a request to the API endpoint and stores the returned response object in a variable
4Check that the request succeeded before using the data (200 means success) Dif response.status_code == 200:
5response = requests.get(url) # no import requests at the top Erequests.get(url, params={"city": "Melbourne"})
6if response.status_code == "200": FThe requests library was never imported, so Python raises a NameError because requests is not defined. Add import requests at the top.

Definitions

requests.get(url)

Fill in the Blanks

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

1.
______ requests
2.
response = requests.___(url)
3.
params = {"city": "Melbourne"} r = requests.get(url, ______=params)
4.
url = "https://api.example.com/data" r = requests.get(___)
5.
if r.___________ == 200: data = r.json()

Spot the Error

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

Question 1
A import request
B url = "https://api.x.com/v1"
C r = requests.get(url)
D print(r.status_code)
Question 2
A import requests
B url = https://api.x.com/v1
C r = requests.get(url)
D print(r.status_code)
Question 3
A import Requests
B u = "https://api.x.com/v1"
C p = {"city": "Perth"}
D r = requests.get(u, params=p)
Question 4
A import requests
B url = "https://api.x.com/v1"
C response = requests.get(url)
D print(response.status_code())
Question 5
A import requests
B url = "https://api.x.com/v1"
C r = requests.get(url)
D if r.status_code == "200":
E print("Success")

Answer Key

Calling a web API with requests.get()
Section A: Quick Questions
Q1
A — response = requests.get(url)
Q2
A — import requests
Q3
B — A response object
Q4
A — 200
Q5
B — if response.status_code == 200:
Section B: Match the Pairs
Q1
import requests → Loads the third-party requests library so the program can send web requests
Q2
response = requests.get(url) → Sends a request to the API endpoint and stores the returned response object in a variable
Q3
Pass extra parameters to an API by giving requests.get() a params dictionary → requests.get(url, params={"city": "Melbourne"})
Q4
Check that the request succeeded before using the data (200 means success) → if response.status_code == 200:
Q5
response = requests.get(url) # no import requests at the top → The requests library was never imported, so Python raises a NameError because requests is not defined. Add import requests at the top.
Q6
if response.status_code == "200": → status_code is an integer, so comparing it to the string "200" is never true. It should be compared to the number 200.
Section C: Definitions
Q1
requests.get(url) — Sends a request to a web API endpoint at the given URL and returns a response object. Needs import requests. Check response.status_code (200 means success) before using the data.
Section D: Fill in the Blanks
Q1
import requests
Q2
response = requests.get(url)
Q3
params = {"city": "Melbourne"} r = requests.get(url, params=params)
Q4
url = "https://api.example.com/data" r = requests.get(url)
Q5
if r.status_code == 200: data = r.json()
Section E: Spot the Error
Q1
A — The library is called requests, with an s. 'import request' causes a ModuleNotFoundError. It should be 'import requests'.
Q2
B — The URL is text, so it must be wrapped in quotes. Without them Python raises a syntax error. It should be url = "https://api.x.com/v1".
Q3
A — Python is case-sensitive, so the module name must be lowercase. 'import Requests' fails. It should be 'import requests'.
Q4
D — status_code is an attribute that holds a number, not a method. Adding () tries to call an int and causes a TypeError. It should be print(response.status_code).
Q5
D — status_code is an integer, so comparing it to the string "200" is never True. It should be if r.status_code == 200:.