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

Project: Delete Request

πŸ—‘οΈ Understanding the DELETE Request

The DELETE HTTP request method is used to remove data from the server

  • πŸ‘‰ Deletes an existing resource permanently

  • πŸ—‚οΈ Commonly used when a client needs to remove a specific resource

  • ⚠️ Once deleted, the resource is gone unless recovery is explicitly implemented


🌐 Request URL Structure

127.0.0.1:8000/books/delete_book/{book_title}
  • πŸ“ Path parameter {book_title} identifies which book should be deleted

  • πŸ‘† The book title is passed directly in the URL for precise deletion


πŸ› οΈ Implementing DELETE in FastAPI

πŸ—‚οΈ books.py

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

@app.delete("/books/delete_book/{book_title}")
async def delete_book(book_title: str):
    # πŸ‘‰ Loop through all books to find the matching title
    for i in range(len(BOOKS)):
        if BOOKS[i].get('title').casefold() == book_title.casefold():
            # πŸ‘‰ Remove the matched book from the list
            BOOKS.pop(i)
            break

πŸ”Ž How This Works

  • 🌐 DELETE /books/delete_book/{book_title} β†’ Endpoint to delete a book by title

  • πŸ”— book_title β†’ Path parameter from the URL

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

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

  • ❌ If matched β†’ removes the book using pop(i)

  • β›” break β†’ Stops the loop after deleting the first match


⚠️ Important Notes

  • DELETE requests typically do not have a request body

  • Deleted resources are permanently removed

  • In production, consider adding error handling or confirmation mechanisms

  • DELETE operations should be used carefully to avoid accidental data loss


🌐 Request URL Example

127.0.0.1:8000/books/delete_book/book%20four
  • Replace book four with the actual title of the book you want to delete

  • %20 β†’ Represents a space in URLs


πŸ§ͺ Testing in the Browser (Swagger UI)

Open your browser and navigate to:

<http://127.0.0.1:8000/docs>
  • πŸ”˜ Select the DELETE endpoint

  • βœ‹ Pass the book title in the path parameter field

  • ▢️ Click Execute to remove the book from the list

After deletion, use a GET request to confirm that the book has been successfully removed.

notion (online-video-cutter.com) (1).mp4


πŸ—‚οΈ 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

@app.delete("/books/delete_book/{book_title}")
async def delete_book(book_title: str):
    for i in range(len(BOOKS)):
        if BOOKS[i].get('title').casefold() == book_title.casefold():
            BOOKS.pop(i)
            break