0
0
Rest APIprogramming~30 mins

PUT for full replacement in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
PUT for Full Replacement in REST API
📖 Scenario: You are building a simple REST API for managing a list of books in a library. Each book has an id, title, and author. You want to allow users to update a book's information completely using the HTTP PUT method.
🎯 Goal: Create a REST API endpoint that uses PUT to fully replace a book's details by its id. The API should update the book if it exists or return an error if it does not.
📋 What You'll Learn
Create a list of books with exact entries
Add a variable to hold the book ID to update
Write a function to replace the book details using PUT logic
Print the updated list of books after replacement
💡 Why This Matters
🌍 Real World
APIs often need to update entire records when users send new data. The PUT method replaces the full resource, which is common in web services.
💼 Career
Understanding how to implement PUT for full replacement is essential for backend developers working with REST APIs in many industries.
Progress0 / 4 steps
1
DATA SETUP: Create the initial list of books
Create a list called books with these exact dictionaries: {'id': 1, 'title': '1984', 'author': 'George Orwell'}, {'id': 2, 'title': 'To Kill a Mockingbird', 'author': 'Harper Lee'}, and {'id': 3, 'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald'}.
Rest API
Need a hint?

Use a list of dictionaries. Each dictionary represents a book with keys 'id', 'title', and 'author'.

2
CONFIGURATION: Set the book ID to update
Create a variable called book_id_to_update and set it to 2 to represent the book you want to replace.
Rest API
Need a hint?

Just create a variable with the exact name book_id_to_update and assign the number 2.

3
CORE LOGIC: Write the PUT replacement function
Write a function called put_replace_book that takes books, book_id, and a new book_data dictionary. Use a for loop with variables index and book to find the book with id equal to book_id. Replace the entire book dictionary at that index with book_data and return True. If not found, return False. Then call put_replace_book with books, book_id_to_update, and {'id': 2, 'title': 'Brave New World', 'author': 'Aldous Huxley'}.
Rest API
Need a hint?

Use enumerate to get both index and book. Replace the book at the found index with the new dictionary.

4
OUTPUT: Print the updated list of books
Write print(books) to display the updated list of books after the replacement.
Rest API
Need a hint?

Use print(books) to show the updated list.