Bird
Raised Fist0
Rest APIprogramming~30 mins

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

Choose your learning style10 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
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
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
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
Hint

Use print(books) to show the updated list.

Practice

(1/5)
1. What does the HTTP PUT method do when used on a REST API resource URL?
easy
A. Retrieves the resource data from the URL
B. Partially updates the resource fields at the URL
C. Fully replaces the resource at the URL with the provided data
D. Deletes the resource at the URL

Solution

  1. Step 1: Understand PUT method purpose

    PUT is designed to replace the entire resource at the given URL with the data sent in the request.
  2. Step 2: Compare with other methods

    Unlike PATCH which updates partially, PUT replaces fully. GET retrieves, DELETE removes.
  3. Final Answer:

    Fully replaces the resource at the URL with the provided data -> Option C
  4. Quick Check:

    PUT = full replacement [OK]
Hint: PUT replaces entire resource, not partial update [OK]
Common Mistakes:
  • Confusing PUT with PATCH partial update
  • Thinking PUT deletes resource
  • Assuming PUT only retrieves data
2. Which of the following is the correct HTTP method and usage to fully replace a resource at URL /users/123 with new data in JSON format?
easy
A. POST /users/123 with JSON body
B. PUT /users/123 with JSON body
C. PATCH /users/123 with JSON body
D. GET /users/123 with JSON body

Solution

  1. Step 1: Identify method for full replacement

    PUT is the HTTP method used to fully replace a resource at a specific URL.
  2. Step 2: Match method with URL and body

    PUT /users/123 with JSON body replaces the user data fully. POST is for creation, PATCH partial update, GET retrieves.
  3. Final Answer:

    PUT /users/123 with JSON body -> Option B
  4. Quick Check:

    PUT + URL + JSON = full replace [OK]
Hint: Use PUT with URL and full JSON body to replace resource [OK]
Common Mistakes:
  • Using POST instead of PUT for replacement
  • Using PATCH for full replacement
  • Sending body with GET request
3. Given a REST API with a user resource at /users/42 initially having data {"name": "Alice", "age": 30}, what will be the resource data after this PUT request?
PUT /users/42
{"name": "Bob"}
medium
A. {"name": "Bob"}
B. {"name": "Bob", "age": 30}
C. {"age": 30}
D. Resource deleted

Solution

  1. Step 1: Understand PUT replaces entire resource

    The PUT request replaces the whole resource data with the new JSON body, removing any fields not included.
  2. Step 2: Apply replacement to given data

    Original data had name and age, but PUT body only has name "Bob", so age is removed.
  3. Final Answer:

    {"name": "Bob"} -> Option A
  4. Quick Check:

    PUT replaces all fields, missing fields removed [OK]
Hint: PUT replaces all fields; missing fields are removed [OK]
Common Mistakes:
  • Assuming PUT merges fields like PATCH
  • Expecting old fields to remain if not sent
  • Thinking PUT deletes resource
4. You wrote this PUT request to update a resource:
PUT /items/10
{"price": 100}

But after sending, the resource lost all other fields except price. How can you fix this to keep other fields intact?
medium
A. Use GET method instead
B. Add missing fields to the PUT JSON body
C. Send an empty body with PUT
D. Change PUT to PATCH to update partially

Solution

  1. Step 1: Identify cause of lost fields

    PUT replaces the entire resource, so missing fields in body are removed.
  2. Step 2: Choose method to update partially

    PATCH updates only specified fields, keeping others intact.
  3. Final Answer:

    Change PUT to PATCH to update partially -> Option D
  4. Quick Check:

    PATCH = partial update, keeps other fields [OK]
Hint: Use PATCH for partial update, PUT replaces all [OK]
Common Mistakes:
  • Trying to fix by adding missing fields manually
  • Sending empty body with PUT
  • Using GET to update resource
5. You want to update a user resource at /users/77 with new data {"name": "Eve", "age": 28}. The resource may or may not exist. Which is the best approach using PUT to ensure the resource is created if missing or fully replaced if exists?
hard
A. Send a PUT request with full user data to /users/77
B. Send a POST request to /users with partial data
C. Send a PATCH request to /users/77 with partial data
D. Send a GET request to /users/77 before updating

Solution

  1. Step 1: Understand PUT behavior for create or replace

    PUT can create the resource if it does not exist, or fully replace it if it does.
  2. Step 2: Apply PUT with full data to target URL

    Sending PUT with full user data to /users/77 ensures creation or full replacement.
  3. Final Answer:

    Send a PUT request with full user data to /users/77 -> Option A
  4. Quick Check:

    PUT = create or replace resource fully [OK]
Hint: PUT creates or replaces resource at URL with full data [OK]
Common Mistakes:
  • Using POST which creates new resource with new ID
  • Using PATCH which only updates existing resource partially
  • Using GET before update unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard