π 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
BOOKSlist 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_bookAll 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