← QCE Digital Solutions Unit 4

Sending data with requests.post()

Sends a Python dictionary to a web API as a JSON request body, unlike GET which only fetches data. Check the response with response.status_code.

Understanding Sending data with requests.post() in QCE Digital Solutions Unit 4

What it does: POST carries a body of data to the API, while requests.get() only asks for data and does not send a JSON body. A status code of 201 means the new data was created, so comparing status_code with 201 confirms the POST worked. An API normally expects a JSON object, and a Python dictionary is converted into one. A lone number is not a record with named fields. 404 means Not Found, which suggests the URL is wrong. Success codes are in the 200s, such as 200 or 201.

Common mistakes: The library is called requests (with an s). 'import request' raises a ModuleNotFoundError, so requests.post() can never run. It should be 'import requests'. Key-value pairs need a dictionary, which uses curly braces. Square brackets make a list, and a list cannot hold name: value pairs. It should be d = {"name": "Ava", "pts": 9}.

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 334–401) and word search (Modules 127–401).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which call sends the dictionary data to a web API as a JSON request body?
requests.post(url, json=data)requests.get(url, json=data)
Matches
requests.get(url, json=data)
↓
requests.get() is used to fetch data, not send it. To send data as a JSON body, use requests.post().
Applying
Meanings
Term
requests.post(url, json=data)
Meaning
Sends a Python dictionary to a web API as a JSON request body, unlike GET which only fetches data. Check the response with response.status_code.
Practice
import requests
Testing
Gaps
response = requests.(url, json=data)
Check Answer
Spots
1import request
2url = "https://api.io/notes"

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

The 'Moose' badge — Tundra Champion, 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 call sends the dictionary data to a web API as a JSON request body?
A. requests.post(url, json=data) ✓
B. requests.get(url, json=data)
POST is used to send data to an API. GET is mainly used to fetch data, so it is the wrong choice for sending a JSON body.
2. What does the json= argument in requests.post(url, json=data) do?
A. It converts the dictionary to JSON and sends it as the request body ✓
B. It reads JSON back from the API response
json=data packages your Python dictionary as JSON in the request body. Reading the reply is done with response.json().
3. Which request type would you choose to submit a new student record to an API?
A. POST ✓
B. GET
POST sends new data to the server. GET only asks the server for data that already exists.
4. Which line checks whether the server accepted your POST request?
A. print(response.post_code)
B. print(response.status_code) ✓
The response object stores the HTTP result in status_code, for example 200 or 201 for success. There is no post_code attribute.
5. Which is the correct way to store the server's reply to a POST?
A. response = requests.post(url) = json(data)
B. response = requests.post(url, json=data) ✓
Assign the result of requests.post() to a variable, with url and json=data as arguments inside the brackets. The other line is not valid Python.

Match the Pairs

requests.post(url, json=data) → Sends the dictionary data to the API at url as a JSON request body
response.status_code → Gives the number the API sent back, such as 201, to show whether the request worked
GET only fetches data from an API, so to send a dictionary to an API you use POST instead → requests.post(url, json=student)
After sending data, check that the API accepted it by testing the status code → if response.status_code == 201:
requests.get(url, json=data) → requests.get() is used to fetch data, not send it. To send data as a JSON body, use requests.post().
requests.post(url, data) → The dictionary is passed without the json= keyword, so requests does not send it as a JSON body. It should be json=data.

Key Term

requests.post(url, json=data)
Sends a Python dictionary to a web API as a JSON request body, unlike GET which only fetches data. Check the response with response.status_code.

Fill in the Blanks

1. response = requests.post(url, json=data)
Hint: Type the requests function that sends data to an API, rather than just fetching it.
2. response = requests.post(url, json=data)
Hint: Type the keyword argument name that turns the dictionary into a JSON request body.
3. if response.status_code == 201: print("Saved")
Hint: Type the response attribute that holds the numeric result of the request.
4. info = requests.get(url) # fetch only
Hint: Type the function that only fetches data and sends no JSON body.
5. data = {"name": "Ava", "score": 95} response = requests.post(url, json=data) print(response.status_code)
Hint: Type the variable name that holds the dictionary being sent.

Spot the Error

Question 1
A import request
B url = "https://api.io/notes"
C d = {"title": "Quiz"}
D r = requests.post(url, json=d)
E print(r.status_code)
Line A — The library is called requests (with an s). 'import request' raises a ModuleNotFoundError, so requests.post() can never run. It should be 'import requests'.
Question 2
A import requests
B url = "https://api.io/scores"
C d = ["name": "Ava", "pts": 9]
D r = requests.post(url, json=d)
E print(r.status_code)
Line C — Key-value pairs need a dictionary, which uses curly braces. Square brackets make a list, and a list cannot hold name: value pairs. It should be d = {"name": "Ava", "pts": 9}.
Question 3
A import requests
B url = "https://api.io/users"
C d = {"user": "Zoe", "age": 17}
D r = requests.get(url, json=d)
E print(r.status_code)
Line D — requests.get() fetches data and is not meant for sending a JSON body. To send data to the API you must use requests.post(url, json=d).
Question 4
A import requests
B url = "https://api.io/orders"
C d = {"item": pen, "qty": 2}
D r = requests.post(url, json=d)
E if r.status_code == 201:
F print("Saved")
Line C — The value pen has no quotes, so Python treats it as a variable name and raises a NameError. Text values in the dictionary must be strings: "pen".
Question 5
A import post from requests
B url = "https://api.io/items"
C d = {"name": "Mug", "qty": 3}
D res = post(url, json=d)
E print(res.status_code)
Line A — Python's syntax is 'from module import name'. 'import post from requests' is not valid Python. It should be 'from requests import post'.

Student with a class code? Join your class →