0
0
Rest APIprogramming~30 mins

Query parameters for filtering in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Query Parameters for Filtering in REST API
📖 Scenario: You are building a simple REST API for a bookstore. The API should allow users to get a list of books and filter them by genre using query parameters.
🎯 Goal: Build a REST API endpoint that returns a filtered list of books based on the genre query parameter.
📋 What You'll Learn
Create a list of books with title and genre
Add a query parameter variable to capture the genre filter
Filter the list of books by the genre query parameter
Return the filtered list as JSON output
💡 Why This Matters
🌍 Real World
Filtering data using query parameters is common in APIs to let users get only the information they want.
💼 Career
Understanding query parameters and filtering is essential for backend developers working with REST APIs.
Progress0 / 4 steps
1
Create the initial list of books
Create a list called books with these exact dictionaries: {'title': 'The Hobbit', 'genre': 'Fantasy'}, {'title': '1984', 'genre': 'Dystopian'}, {'title': 'To Kill a Mockingbird', 'genre': 'Classic'}, {'title': 'The Great Gatsby', 'genre': 'Classic'}
Rest API
Need a hint?

Use a list of dictionaries to store the books with their titles and genres.

2
Add a query parameter variable for genre
Create a variable called genre_filter and set it to the string 'Classic' to simulate receiving a query parameter.
Rest API
Need a hint?

Set the genre_filter variable to the genre you want to filter by.

3
Filter the books list by genre
Create a new list called filtered_books using a list comprehension that includes only books where the genre matches genre_filter.
Rest API
Need a hint?

Use a list comprehension to select books where book['genre'] equals genre_filter.

4
Print the filtered books list
Use print(filtered_books) to display the filtered list of books.
Rest API
Need a hint?

Use print(filtered_books) to show the filtered list.