๐ Filtering Books by Category (Query Parameter)
Letโs filter books by category using a query parameter.
from fastapi import FastAPI
app = FastAPI()
BOOKS = ...
#API endpoint that filters BOOKS by category
@app.get("/books/")
async def read_category_by_query(category: str):
books_to_return = []
for book in BOOKS:
if book.get('category').casefold() == category.casefold():
books_to_return.append(book)
return books_to_return๐ง Whatโs Happening Here?
๐ Loops through all books
๐ Checks for books matching the category from the query parameter
๐ค Returns a list of matching books
FastAPI automatically detects:
Anything passed after
/books/that is not part of the pathIs treated as a query parameter
async def read_category_by_query(category: str):๐ท๏ธ
categoryโ Query parameter name๐ค
strโ Only string values are allowed
๐ Viewing the Result in the Browser
Example: Retrieve books in the math category.
127.0.0.1:8000/books/?category=math โ
/books/โ Pathโ
?category=mathโ Query parameter
Returns all books that belong to the math category.
notion (2).mp4
๐ Combining Path and Query Parameters
Query parameters can be combined with path parameters for more specific queries.
So far:
๐ Path parameter โ Retrieve books by title or author
๐ Query parameter โ Filter books by category
Now, letโs use both together.
๐งช Filtering by Author (Path) and Category (Query)
#API Endpoint with a python function that filters BOOKS by Author and Category
@app.get("/books/{book_author}/")
async def read_author_and_category(book_author: str, category: str):
books_to_return = []
for book in BOOKS:
if book.get('author').casefold() == book_author.casefold() and \\
book.get('category').casefold() == category.casefold():
books_to_return.append(book)
return books_to_return๐ Viewing the Result in the Browser
Example: Retrieve specific books by a specific author and category
127.0.0.1:8000/books/author%20four/?category=science ๐ง URL Breakdown
/books/author%20fourโ Path parameter (book_author)?category=scienceโ Query parameter (category)%20โ Represents a space in the URL
๐ What This Allows Us to Do
Retrieve books by a specific author
Filter results further by category
Easily try different authors and categories for dynamic responses
Returns an
empty listif no books match
โ Combining Path + Query parameters is very common in GET requests and makes APIs flexible and user-friendly.
from fastapi import FastAPI
app = FastAPI()
BOOKS = [
{"title": "Title One", "author": "Author One", "category": "science"},
{"title": "Title Two", "author": "Author Two", "category": "science"},
{"title": "Title Three", "author": "Author Three", "category": "history"},
{"title": "Title Four", "author": "Author Four", "category": "math"},
{"title": "Title Five", "author": "Author Five", "category": "math"},
]
@app.get("/books")
async def read_all_books():
return BOOKS
@app.get('/books/{book_title}')
async def read_book(book_title: str):
for book in BOOKS:
if book.get('title').casefold() == book_title.casefold():
return book
@app.get("/books/")
async def read_category_by_query(category: str):
books_to_return = []
for book in BOOKS:
if book.get('category').casefold() == category.casefold():
books_to_return.append(book)
return books_to_return
@app.get("/books/{book_author}/")
async def read_author_and_category(book_author: str, category: str):
books_to_return = []
for book in BOOKS:
print('book', book)
if book.get('author').casefold() == book_author.casefold() and \\
book.get('category').casefold() == category.casefold():
books_to_return.append(book)
return books_to_return