Module 03 / Project 01 — First API Call¶
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | Walkthrough | — | Flashcards | — | — |
Focus¶
- Making a GET request with
requests.get() - Understanding response objects (status code, headers, body)
- Parsing JSON responses into Python dictionaries
Why this project exists¶
Before you can build anything that talks to the internet, you need to understand the basic request-response cycle. This project strips it down to the simplest possible case: fetch one resource, look at what comes back, and pull out the pieces you care about.
Run¶
Expected output¶
--- Raw JSON response ---
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
--- Accessing individual fields ---
Status code : 200
Post ID : 1
User ID : 1
Title : sunt aut facere repellat provident occaecati excepturi optio reprehenderit
Body preview: quia et suscipit...
--- Response headers (selected) ---
Content-Type: application/json; charset=utf-8
Alter it¶
- Change the URL to fetch post
/posts/5instead of/posts/1. Run again and compare the output. - Print the full
bodyfield instead of just the first 20 characters. - Add a line that prints the number of characters in the title.
Break it¶
- Change the URL to
https://jsonplaceholder.typicode.com/posts/99999(a post that does not exist). What status code do you get? What doesresponse.json()return? - Remove the
.json()call and printresponse.textinstead. What is the difference? - Change the URL to an invalid domain like
https://not-a-real-domain-xyz.com/posts/1. What error do you get?
Fix it¶
- Add a check: if
response.status_codeis not 200, print a warning and skip the JSON parsing. - Wrap the
requests.get()call in atry/exceptblock that catchesrequests.exceptions.ConnectionErrorand prints a friendly message instead of a traceback. - After fixing, run with both a valid and invalid URL to confirm both paths work.
Explain it¶
- What does
response.json()actually do under the hood? - What is the difference between
response.textandresponse.json()? - Why does
requests.get()return a Response object instead of just the data? - What HTTP method does
requests.get()use, and what does that method mean?
Mastery check¶
You can move on when you can:
- write a GET request from memory without looking at docs,
- explain what a status code of 200 means vs 404,
- extract any field from a JSON response,
- describe the difference between
.textand.json().
Related Concepts¶
Next¶
Continue to Query Parameters.