← VCE Software Development U4O1

Calling a web API with requests.get()

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.

Understanding Calling a web API with requests.get() in VCE Software Development U4O1

What it does: The params dictionary is added to the URL as a query string, so the API knows which data to send back. response.status_code holds the result code of the request. A value of 200 means success, so the program can safely use the data. requests.get(url) sends a GET request to the URL and returns a response object. There is no requests.send() function. The library is called requests (with an s). You import it once, then call requests.get(url) in your code. 200 means OK, the request worked. 404 means the resource was not found. Parameters start after a ? in the URL. Extra parameters are then joined with &.

Common mistakes: The library is called requests, with an s. 'import request' causes a ModuleNotFoundError. It should be 'import requests'. 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".

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 115–124) and word search (Modules 115–171).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line of code sends a request to a web API?
response = requests.send(url)response = requests.get(url)
Matches
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.
Applying
Meanings
Term
requests.get(url)
Meaning
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.
Practice
import requests
Testing
Gaps
requests
Check Answer
Spots
1import request
2url = "https://api.x.com/v1"

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Reef Shark' badge — Reef Ruler, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line of code sends a request to a web API?
A. response = requests.get(url) ✓
B. response = requests.send(url)
requests.get(url) sends a GET request to the URL and returns a response object. There is no requests.send() function.
2. What must you write at the top of your program before using requests.get()?
A. import requests ✓
B. import request.get
The library is called requests (with an s). You import it once, then call requests.get(url) in your code.
3. What does requests.get(url) return?
A. Only the plain text of the web page
B. A response object ✓
It returns a response object. That object holds the status code, the data and other details about the reply.
4. Which status code means the API request was successful?
A. 200 ✓
B. 404
200 means OK, the request worked. 404 means the resource was not found.
5. Which line checks whether the request succeeded?
A. if requests.status_code == 200:
B. if response.status_code == 200: ✓
status_code belongs to the response object that requests.get() returned, not to the requests library itself.

Match the Pairs

import requests → Loads the third-party requests library so the program can send web requests
response = requests.get(url) → Sends a request to the API endpoint and stores the returned response object in a variable
Pass extra parameters to an API by giving requests.get() a params dictionary → requests.get(url, params={"city": "Melbourne"})
Check that the request succeeded before using the data (200 means success) → if response.status_code == 200:
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.
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.

Key Term

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.

Fill in the Blanks

1. import requests
Hint: Type the keyword that brings a library into your program.
2. response = requests.get(url)
Hint: Type the function name that sends a request to fetch data from a URL.
3. params = {"city": "Melbourne"} r = requests.get(url, params=params)
Hint: Type the keyword argument name used to send query parameters with the request.
4. url = "https://api.example.com/data" r = requests.get(url)
Hint: Type the variable name that holds the web address passed to the request.
5. if r.status_code == 200: data = r.json()
Hint: Type the response attribute that holds the number showing whether the request worked.

Spot the Error

Question 1
A import request
B url = "https://api.x.com/v1"
C r = requests.get(url)
D print(r.status_code)
Line A — The library is called requests, with an s. 'import request' causes a ModuleNotFoundError. It should be 'import requests'.
Question 2
A import requests
B url = https://api.x.com/v1
C r = requests.get(url)
D print(r.status_code)
Line 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".
Question 3
A import Requests
B u = "https://api.x.com/v1"
C p = {"city": "Perth"}
D r = requests.get(u, params=p)
Line A — Python is case-sensitive, so the module name must be lowercase. 'import Requests' fails. It should be 'import requests'.
Question 4
A import requests
B url = "https://api.x.com/v1"
C response = requests.get(url)
D print(response.status_code())
Line 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).
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")
Line D — status_code is an integer, so comparing it to the string "200" is never True. It should be if r.status_code == 200:.

Student with a class code? Join your class →