Module 03 / Project 02 — Query Parameters¶
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | — |
Focus¶
- Passing query parameters with the
paramsdict - Filtering API results by field values
- Paginating results with
_limitand_start - Understanding the difference between params dict and URL string
Why this project exists¶
Most APIs return too much data if you do not tell them what you want. Query parameters are how you filter, sort, and paginate results. This project teaches you two ways to attach parameters to a request and shows you how to work with lists of results instead of a single resource.
Run¶
Expected output¶
--- Method 1: params dict ---
Fetching posts by user 3 (using params dict)...
Found 10 posts by user 3.
Post 21: ea molestias quasi exercitationem repellat qui ipsa sit aut
Post 22: delectus ullam et corporis nulla voluptas sequi
Post 23: nesciunt iure omnis dolorem tempora et accusantium
(showing first 3)
--- Method 2: URL string ---
Fetching posts by user 3 (using URL string)...
Found 10 posts by user 3.
--- Pagination ---
Page 1: fetched 5 posts (IDs: 1, 2, 3, 4, 5)
Page 2: fetched 5 posts (IDs: 6, 7, 8, 9, 10)
Page 3: fetched 5 posts (IDs: 11, 12, 13, 14, 15)
Alter it¶
- Change the
userIdfilter to a different user (1 through 10 are valid). How many posts does each user have? - Change the page size from 5 to 3 and fetch 5 pages instead of 3.
- Combine filtering and pagination: fetch user 1's posts, 2 at a time.
Break it¶
- Pass
userId=999— a user that does not exist. What does the API return? Is it an error or an empty list? - Set
_limit=0. What happens? - Pass
params={"userId": [1, 2]}— a list instead of a single value. Does the API handle it? What do you get back?
Fix it¶
- Add a check after each request: if the response is an empty list, print "No results found" instead of trying to iterate.
- Add validation before the request: if
user_idis not between 1 and 10, print a warning. - After fixing, test with both valid and invalid user IDs.
Explain it¶
- What is the difference between passing
params={"userId": 3}and putting?userId=3directly in the URL string? - Why does JSONPlaceholder return an empty list instead of a 404 when you filter by a nonexistent user?
- How does
_startdiffer from_pagein pagination schemes you might encounter in other APIs? - When would you choose params dict over URL string in your own code?
Mastery check¶
You can move on when you can:
- pass query parameters using both methods,
- paginate through results using
_startand_limit, - handle empty results gracefully,
- explain why the params dict is usually preferred.
Related Concepts¶
Next¶
Continue to POST and Auth.