FastAPI - Beginner to Pro / Project 1 - FastAPI Request Method Logic

Project: Query Parameters

๐Ÿ“š 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 path

  • Is 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 list if 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