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

Project: PUT Request

πŸ”„ Understanding the PUT Request

The PUT HTTP request method is used to update existing resources on the server

  • Unlike POST (which creates new data), PUT is used to replace or update existing resources

  • PUT is idempotent β€” sending the same request multiple times results in the same final state

  • Like POST, PUT supports sending data through a request body


πŸ“¦ Example Request Body

{
    "title": "Title Six",
    "author": "Author Two",
    "category": "science"
}

πŸ‘‰ This JSON object represents the updated version of an existing book


πŸ› οΈ Implementing Book Updates in FastAPI

πŸ—‚οΈ books.py

from fastapi import FastAPI, Body

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"},
    {"title": "Title Six", "author": "Author Two", "category": "math"}
]

@app.put("/books/update_book")
async def update_book(updated_book=Body()):
    # πŸ‘‰ Loop through BOOKS to find the matching title
    for i in range(len(BOOKS)):
        if BOOKS[i].get('title').casefold() == \\
                updated_book.get('title').casefold():
            # πŸ‘‰ Replace existing book with updated data
            BOOKS[i] = updated_book

πŸ”Ž How This Works

  • 🌐 PUT /books/update_book β†’ Endpoint to update an existing book

  • πŸ“₯ updated_book = Body()

    πŸ‘‰ Receives updated book data from the request body

  • πŸ” Loops through the BOOKS list using index

  • 🧠 Checks if the book title matches (case-insensitive)

  • πŸ”„ If a match is found β†’ replaces the old book with the updated one

🌐 Request URL

  • URL: 127.0.0.1:8000/books/update_book

  • All updated information is transmitted securely in the request body

πŸ“Œ Example:

Updating the category of Book Six from math to history

  • Once executed, the updated book details will appear when fetching all books

Untitled design (24) (1) (online-video-cutter.com).mp4


⚑ Key Characteristics of PUT

  • Used to update existing resources

  • Requires a complete representation of the resource

  • Supports request bodies (like POST)

  • Idempotent β€” repeated requests result in the same final state

  • Does not create duplicate data


πŸ—‚οΈ books.py

from fastapi import FastAPI, Body

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"},
    {"title": "Title Six", "author": "Author Two", "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

@app.post("/books/create_book")
async def create_book(new_book=Body()):
    BOOKS.append(new_book)

@app.put("/books/update_book")
async def update_book(updated_book=Body()):
    for i in range(len(BOOKS)):
        if BOOKS[i].get('title').casefold() == \\
                updated_book.get('title').casefold():
            BOOKS[i] = updated_book