Practice: GET and POST Requests

« Read the chapter

Q1. When to use GET

BeginnerConceptual

Name one situation where you should use GET.

Goal: Choose GET.

Q2. When to use POST

BeginnerConceptual

Name one situation where you should use POST.

Goal: Choose POST.

Q3. GET URL with params

BeginnerCoding

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

BeginnerCoding

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

IntermediateCoding

Prepare a POST to https://x.io/u with json {'ok': True} and print the body.

Goal: Send JSON.

Q6. Content type of json

IntermediateCoding

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

IntermediateCoding

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

BeginnerOutput

What does this print?

Code
import requests
p = requests.Request("POST", "https://x.io/u", json={}).prepare()
print(p.method)

Goal: Read prepared requests.

Q9. Form body output

IntermediateOutput

What does this print?

Code
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

IntermediateOutput

What does this print?

Code
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

IntermediateOutput

What does this print?

Code
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

AdvancedOutput

What does this print?

Code
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

IntermediateDebugging

This code should send JSON but sends form text. Fix it.

Code
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

IntermediateDebugging

This code puts search options in a body of a GET. Fix it so the options go in the URL.

Code
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

AdvancedConceptual

A script sends a password with GET in the URL. What should change?

Goal: Protect secrets.

Q16. Success code

BeginnerConceptual

Which status code usually comes back when POST creates a new item?

Goal: Know POST results.

Q17. Duplicate risk

AdvancedConceptual

Why can retrying a failed POST be risky?

Goal: Understand idempotency.

Q18. Sign up call

IntermediateApplication

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

BeginnerApplication

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.