Practice: Requests Library

« Read the chapter

Q1. Install the library

BeginnerConceptual

Write the terminal command that installs requests.

Goal: Install a package.

Q2. Prepare a URL

IntermediateCoding

Use requests.Request to prepare a GET to https://x.io/s with params {'q': 'cat'} and print the URL.

Goal: Use params.

Q3. Params with two keys

IntermediateCoding

Prepare a GET to https://x.io/list with params {'a': 1, 'b': 'two'} and print the URL.

Goal: Use params.

Q4. Escaped space

IntermediateOutput

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.

Q5. Prepared headers

IntermediateOutput

What does this print?

Code
import requests
p = requests.Request("GET", "https://x.io", headers={"X-Id": "7"}).prepare()
print(p.headers["X-Id"], p.method)

Goal: Send headers.

Q6. Case insensitive headers

IntermediateOutput

What does this print?

Code
import requests
p = requests.Request("GET", "https://x.io", headers={"Accept": "text/html"}).prepare()
print(p.headers["accept"])

Goal: Know header behaviour.

Q7. Build a Response by hand

AdvancedCoding

Create requests.models.Response(), set status_code to 404, and print its ok attribute.

Goal: Understand Response.ok.

Q8. Raise on error

AdvancedCoding

Create a Response with status_code 500 and call raise_for_status inside try. Print 'failed' when HTTPError is raised.

Goal: Handle errors.

Q9. ok for 301

AdvancedOutput

What does this print?

Code
import requests
r = requests.models.Response()
r.status_code = 301
print(r.ok)

Goal: Understand Response.ok.

Q10. json is a method

BeginnerDebugging

This code does not give the data. Fix it.

Code
import requests
r = requests.models.Response()
r._content = b'{"n": 5}'
print(r.json["n"])

Goal: Call json().

Q11. Missing timeout

IntermediateConceptual

What is wrong with requests.get('https://api.example.com/data') in a script that must never hang?

Goal: Use timeouts.

Q12. Do not name it requests.py

AdvancedConceptual

Your file is named requests.py and import requests fails oddly. Why?

Goal: Avoid name clashes.

Q13. Fix the string join

IntermediateDebugging

Fix this code so params builds the URL.

Code
import requests
url = "https://x.io/s?q=" + "a b"
print(url)

Goal: Use params.

Q14. Count the pairs

BeginnerOutput

What does this print?

Code
import requests
p = requests.Request("GET", "https://x.io/s", params={"a": 1, "b": 2, "c": 3}).prepare()
print(p.url.count("&"))

Goal: Read built URLs.

Q15. Choose text or content

IntermediateConceptual

Which attribute should you write to a file when you download a PNG image?

Goal: Choose the right attribute.

Q16. Why Session

IntermediateConceptual

Give one reason to use requests.Session.

Goal: Know Session.

Q17. Catch everything

IntermediateConceptual

Which exception class catches all errors raised by requests?

Goal: Handle exceptions.

Q18. Weather call plan

AdvancedApplication

You call a weather API for city 'Pune' with key stored in a variable. Describe the requests call you would write.

Goal: Plan an API call.

Q19. Round trip a Response

AdvancedCoding

Build a Response whose body is b'{"a": [1, 2]}' and print the second item of a.

Goal: Read JSON bodies.