Practice: GET and POST Requests
Q1. When to use GET
Name one situation where you should use GET.
Goal: Choose GET.
Q2. When to use POST
Name one situation where you should use POST.
Goal: Choose POST.
Q3. GET URL with params
Prepare a GET to https://x.io/find with params {'tag': 'py', 'n': 2} and print the URL.
Goal: Send GET parameters.
Q4. Form body
Prepare a POST to https://x.io/u with data {'a': 1, 'b': 2} and print the body.
Goal: Send form data.
Q5. JSON body
Prepare a POST to https://x.io/u with json {'ok': True} and print the body.
Goal: Send JSON.
Q6. Content type of json
Prepare a POST with json={'a': 1} to https://x.io/u and print its Content-Type header.
Goal: Know JSON headers.
Q7. Form content type
Prepare a POST with data={'a': 1} to https://x.io/u and print its Content-Type header.
Goal: Know form headers.
Q8. Method name
What does this print?
import requests
p = requests.Request("POST", "https://x.io/u", json={}).prepare()
print(p.method)Goal: Read prepared requests.
Q9. Form body output
What does this print?
import requests
p = requests.Request("POST", "https://x.io", data={"n": "Asha", "age": 21}).prepare()
print(p.body)Goal: Read form bodies.
Q10. JSON body output
What does this print?
import requests
p = requests.Request("POST", "https://x.io", json={"ok": True, "n": None}).prepare()
print(p.body)Goal: Read JSON bodies.
Q11. GET has no body
What does this print?
import requests
p = requests.Request("GET", "https://x.io/s", params={"q": 1}).prepare()
print(p.body)Goal: Know GET has no body.
Q12. URL escape
What does this print?
import requests
p = requests.Request("GET", "https://x.io/s", params={"q": "a&b"}).prepare()
print(p.url)Goal: Understand escaping.
Q13. Fix the wrong keyword
This code should send JSON but sends form text. Fix it.
import requests
p = requests.Request("POST", "https://x.io", data={"a": [1, 2]}).prepare()
print(p.headers["Content-Type"])Goal: Pick json= or data=.
Q14. Fix GET with a body
This code puts search options in a body of a GET. Fix it so the options go in the URL.
import requests
p = requests.Request("GET", "https://x.io/s", data={"q": "cat"}).prepare()
print(p.url)Goal: Use params for GET.
Q15. Fix the secret
A script sends a password with GET in the URL. What should change?
Goal: Protect secrets.
Q16. Success code
Which status code usually comes back when POST creates a new item?
Goal: Know POST results.
Q17. Duplicate risk
Why can retrying a failed POST be risky?
Goal: Understand idempotency.
Q18. Sign up call
Write the requests line (as text is fine) that posts a sign up with name Asha as JSON to https://api.example.com/signup with a 5 second timeout.
Goal: Write a POST call.
Q19. Search call
Write the requests line (as text is fine) that searches https://api.example.com/search for the word 'tea' using GET.
Goal: Write a GET call.