ποΈ 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
BOOKSlist 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%20fourReplace
book fourwith 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