0
0
Rest APIprogramming~30 mins

PATCH for partial updates in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
PATCH for Partial Updates in a REST API
📖 Scenario: You are building a simple REST API for managing a list of books in a library. Each book has a title, author, and year published.Sometimes, users want to update only some details of a book, not all. For example, they might want to change just the year published without changing the title or author.
🎯 Goal: Create a REST API endpoint that uses the PATCH method to update only the fields provided in the request for a specific book.This means if the user sends only the year, only that field changes, and the others stay the same.
📋 What You'll Learn
Create a dictionary called books with three books, each having keys title, author, and year.
Create a variable called book_id set to 2 to represent the book to update.
Write code to update only the fields provided in a dictionary called update_data for the book with book_id.
Print the updated book details after applying the partial update.
💡 Why This Matters
🌍 Real World
Partial updates are common in real REST APIs where clients want to change only some details without sending the full data again.
💼 Career
Understanding PATCH requests and partial updates is important for backend developers building APIs that are efficient and user-friendly.
Progress0 / 4 steps
1
Create the initial books data
Create a dictionary called books with these exact entries: 1 maps to {'title': '1984', 'author': 'George Orwell', 'year': 1949}, 2 maps to {'title': 'To Kill a Mockingbird', 'author': 'Harper Lee', 'year': 1960}, and 3 maps to {'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald', 'year': 1925}.
Rest API
Need a hint?
Use a dictionary with integer keys and dictionaries as values for each book.
2
Set the book ID to update
Create a variable called book_id and set it to 2 to represent the book that will be updated.
Rest API
Need a hint?
Just assign the number 2 to the variable book_id.
3
Apply the partial update using PATCH logic
Create a dictionary called update_data with these exact entries: 'year': 1961. Then update only the fields in books[book_id] using update_data so that only the provided fields change.
Rest API
Need a hint?
Use the dictionary update() method to apply partial changes.
4
Print the updated book details
Write a print statement to display the updated book details for books[book_id].
Rest API
Need a hint?
Use print(books[book_id]) to show the updated book.