Sending data with requests.post()

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

Sending data with requests.post()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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)
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
3. Which request type would you choose to submit a new student record to an API?
A POST
B GET
4. Which line checks whether the server accepted your POST request?
A print(response.post_code)
B print(response.status_code)
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)

Match the Pairs

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

Left columnRight column
1requests.post(url, json=data) Aif response.status_code == 201:
2response.status_code BSends the dictionary data to the API at url as a JSON request body
3GET only fetches data from an API, so to send a dictionary to an API you use POST instead CGives the number the API sent back, such as 201, to show whether the request worked
4After sending data, check that the API accepted it by testing the status code DThe dictionary is passed without the json= keyword, so requests does not send it as a JSON body. It should be json=data.
5requests.get(url, json=data) Erequests.post(url, json=student)
6requests.post(url, data) Frequests.get() is used to fetch data, not send it. To send data as a JSON body, use requests.post().

Definitions

requests.post(url, json=data)

Fill in the Blanks

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

1.
response = requests.____(url, json=data)
2.
response = requests.post(url, ____=data)
3.
if response.___________ == 201: print("Saved")
4.
info = requests.___(url) # fetch only
5.
data = {"name": "Ava", "score": 95} response = requests.post(url, json=____) print(response.status_code)

Spot the Error

Shade in the letter of the line that contains an 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)
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)
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)
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")
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)

Answer Key

Sending data with requests.post()
Section A: Quick Questions
Q1
A — requests.post(url, json=data)
Q2
A — It converts the dictionary to JSON and sends it as the request body
Q3
A — POST
Q4
B — print(response.status_code)
Q5
B — response = requests.post(url, json=data)
Section B: Match the Pairs
Q1
requests.post(url, json=data) → Sends the dictionary data to the API at url as a JSON request body
Q2
response.status_code → Gives the number the API sent back, such as 201, to show whether the request worked
Q3
GET only fetches data from an API, so to send a dictionary to an API you use POST instead → requests.post(url, json=student)
Q4
After sending data, check that the API accepted it by testing the status code → if response.status_code == 201:
Q5
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().
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
response = requests.post(url, json=data)
Q2
response = requests.post(url, json=data)
Q3
if response.status_code == 201: print("Saved")
Q4
info = requests.get(url) # fetch only
Q5
data = {"name": "Ava", "score": 95} response = requests.post(url, json=data) print(response.status_code)
Section E: Spot the Error
Q1
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'.
Q2
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}.
Q3
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).
Q4
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".
Q5
A — Python's syntax is 'from module import name'. 'import post from requests' is not valid Python. It should be 'from requests import post'.