📖 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
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
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
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
Hint
Use print(books[book_id]) to show the updated book.
Practice
(1/5)
1. What is the main purpose of using the PATCH method in REST APIs?
easy
A. To update only specific fields of a resource without sending the entire data
B. To create a new resource on the server
C. To delete a resource completely
D. To retrieve data from the server
Solution
Step 1: Understand HTTP methods
HTTP methods like GET, POST, PUT, DELETE, and PATCH have different purposes. PATCH is designed for partial updates.
Step 2: Identify PATCH behavior
PATCH sends only the fields that need to be changed, unlike PUT which requires the full resource data.
Final Answer:
To update only specific fields of a resource without sending the entire data -> Option A
Quick Check:
PATCH updates parts only [OK]
Hint: PATCH updates parts, not whole resource [OK]
Common Mistakes:
Confusing PATCH with PUT which replaces full resource
Thinking PATCH creates new resources
Using PATCH to delete resources
2. Which of the following is the correct way to send a PATCH request to update a user's email using JSON?
easy
A. PATCH /users/123 {"email": "new@example.com"}
B. PATCH /users/123 {"username": "newuser"}
C. GET /users/123 {"email": "new@example.com"}
D. DELETE /users/123 {"email": "new@example.com"}
Solution
Step 1: Identify HTTP method and endpoint
PATCH is used to update part of the resource at /users/123.
Step 2: Check JSON body for correct field
The JSON body contains only the field to update, here "email" with new value.
Final Answer:
PATCH /users/123 {"email": "new@example.com"} -> Option A
Quick Check:
PATCH + partial JSON body = correct syntax [OK]
Hint: PATCH with only fields to update in JSON body [OK]
Common Mistakes:
Using GET or DELETE instead of PATCH
Sending unrelated fields in PATCH body
Trying to update username when email is asked
3. Given this PATCH request body to update a product:
{"price": 19.99}
What will be the result if the original product data was
{"name": "Book", "price": 25.00, "stock": 10}
?
medium
A. {"name": "Book", "price": 25.00, "stock": 10}
B. {"price": 19.99}
C. {"name": "Book", "price": 19.99, "stock": 10}
D. {"name": "Book", "stock": 10}
Solution
Step 1: Understand PATCH partial update
PATCH updates only the fields sent, here "price" changes to 19.99.
Step 2: Combine unchanged fields with updated field
Fields not in PATCH body remain unchanged, so "name" and "stock" stay the same.
Final Answer:
{"name": "Book", "price": 19.99, "stock": 10} -> Option C
Quick Check:
PATCH updates price only, others unchanged [OK]
Hint: PATCH changes only sent fields, keeps others [OK]
Common Mistakes:
Replacing entire object instead of partial update
Removing fields not in PATCH body
Ignoring updated field in result
4. You send this PATCH request to update a user's profile:
{"email": "new@example.com", "age": "twenty"}
The server expects age as an integer. What is the likely problem?
medium
A. The PATCH request will succeed and update both fields
B. The server will delete the age field from the user profile
C. The server will ignore the age field and update only email
D. The server will reject the request due to invalid data type for age
Solution
Step 1: Check data types in PATCH body
"age" is sent as string "twenty" but server expects integer.
Step 2: Understand server validation
Most servers validate data types and reject requests with invalid types.
Final Answer:
The server will reject the request due to invalid data type for age -> Option D
Quick Check:
Invalid data type causes rejection [OK]
Hint: Check data types match server expectations [OK]
Common Mistakes:
Assuming server auto-converts wrong types
Thinking PATCH ignores invalid fields silently
Believing PATCH deletes fields on error
5. You have a REST API for managing tasks with fields: title, completed, and priority. You want to mark a task as completed without changing other fields. Which PATCH request body is best?
hard
A. {"title": "", "completed": true, "priority": null}
B. {"completed": true}
C. {"completed": true, "priority": "high"}
D. {"title": "New Task"}
Solution
Step 1: Identify minimal update needed
Only the "completed" field needs to change, so send only that.
Step 2: Avoid sending unrelated or empty fields
Sending empty or null fields may overwrite data unintentionally.
Final Answer:
{"completed": true} -> Option B
Quick Check:
Send only changed fields in PATCH body [OK]
Hint: Send only fields you want to update in PATCH [OK]
Common Mistakes:
Sending all fields even if unchanged
Sending empty or null values accidentally
Using PUT instead of PATCH for partial update
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.