📖 Scenario: You are building a simple REST API for managing a list of tasks. Each task has an ID and a description. You want to understand how different HTTP methods behave when called multiple times with the same data.
🎯 Goal: Build a simple REST API simulation using Python functions to demonstrate the idempotency of HTTP methods: GET, PUT, POST, and DELETE.
📋 What You'll Learn
Create a dictionary called tasks with initial tasks
Create a variable called new_task with a task description
Write a function put_task that updates or creates a task with a given ID
Write a function post_task that adds a new task with a new ID
Write a function delete_task that deletes a task by ID
Write a function get_task that returns a task by ID
Call each function multiple times to show idempotency behavior
Print the final tasks dictionary
💡 Why This Matters
🌍 Real World
Understanding idempotency helps developers design APIs that behave predictably when clients retry requests, which is common in real-world network communication.
💼 Career
Knowing how HTTP methods behave and their idempotency is essential for backend developers, API designers, and anyone working with web services.
Progress0 / 4 steps
1
Create initial tasks dictionary
Create a dictionary called tasks with these exact entries: 1: 'Buy groceries', 2: 'Read a book', 3: 'Write code'
Rest API
Hint
Use curly braces to create a dictionary with keys 1, 2, and 3 and their task descriptions.
2
Add a new task description variable
Create a variable called new_task and set it to the string 'Go for a walk'
Rest API
Hint
Assign the string 'Go for a walk' to the variable new_task.
3
Write functions to simulate HTTP methods
Write four functions: put_task(id, description) to add or update a task, post_task(description) to add a new task with a new ID, delete_task(id) to remove a task, and get_task(id) to return the task description or None if not found. Use the tasks dictionary inside these functions.
Rest API
Hint
Use dictionary operations to add, update, delete, and get tasks inside the functions.
4
Call functions to demonstrate idempotency and print tasks
Call put_task(2, 'Read a book') twice, call post_task(new_task) twice, call delete_task(3) twice, then print the tasks dictionary.
Rest API
Hint
Calling put_task twice with the same data does not change the dictionary after the first call (idempotent). Calling post_task twice adds two new tasks (not idempotent). Calling delete_task twice removes the task once and does nothing the second time (idempotent).
Practice
(1/5)
1. Which HTTP method is idempotent, meaning it can be called multiple times without changing the result beyond the initial application?
easy
A. POST
B. CONNECT
C. PUT
D. PATCH
Solution
Step 1: Understand idempotency in HTTP methods
Idempotent methods produce the same result no matter how many times they are called with the same input.
Step 2: Identify which methods are idempotent
GET, PUT, and DELETE are idempotent. POST and PATCH are not because they can create or modify resources differently each time.
Final Answer:
PUT -> Option C
Quick Check:
PUT is idempotent = B [OK]
Hint: Remember: PUT replaces, POST creates new [OK]
Common Mistakes:
Confusing POST as idempotent
Thinking PATCH is idempotent
Assuming CONNECT is idempotent
2. Which of the following is the correct syntax to perform an idempotent DELETE request using curl?
easy
A. curl -X DELETE https://api.example.com/resource/123
B. curl -X POST https://api.example.com/resource/123
C. curl -X PUT https://api.example.com/resource/123
D. curl -X GET https://api.example.com/resource/123
Solution
Step 1: Identify the HTTP method for deletion
DELETE is the HTTP method used to remove a resource and is idempotent.
Step 2: Check curl syntax for DELETE
The correct curl command uses -X DELETE followed by the resource URL.
Final Answer:
curl -X DELETE https://api.example.com/resource/123 -> Option A
Quick Check:
DELETE method syntax = C [OK]
Hint: DELETE uses -X DELETE in curl command [OK]
Common Mistakes:
Using POST instead of DELETE
Omitting -X flag for method
Confusing GET with DELETE
3. Consider this sequence of HTTP requests to update a user's email:
PUT /users/42 {"email": "new@example.com"}
PUT /users/42 {"email": "new@example.com"}
What is the expected result after these two requests?
medium
A. User's email remains unchanged after the first update
B. User's email is updated once and remains the same after second request
C. User's email is updated twice, causing duplicate entries
D. An error occurs on the second request
Solution
Step 1: Understand PUT idempotency
PUT replaces the resource with the given data. Multiple identical PUT requests do not change the resource after the first update.
Step 2: Analyze the two identical PUT requests
The first request updates the email. The second request sends the same data, so no further change occurs.
Final Answer:
User's email is updated once and remains the same after second request -> Option B
Quick Check:
PUT idempotent means same result = A [OK]
Hint: PUT with same data does not change resource again [OK]
Common Mistakes:
Assuming PUT creates duplicates
Thinking second PUT causes error
Confusing PUT with POST behavior
4. A developer wrote this code snippet to delete a resource:
Why is this code incorrect for an idempotent delete operation?
medium
A. The response is not handled properly
B. The URL is missing a query parameter
C. The fetch method is missing headers
D. POST is not idempotent and should not be used for deletion
Solution
Step 1: Identify the HTTP method used
The code uses POST to delete a resource, which is incorrect because POST is not idempotent.
Step 2: Understand correct method for deletion
DELETE method should be used for idempotent deletion, ensuring repeated calls do not cause unintended effects.
Final Answer:
POST is not idempotent and should not be used for deletion -> Option D
Quick Check:
DELETE is idempotent, POST is not = A [OK]
Hint: Use DELETE, not POST, for idempotent deletes [OK]
Common Mistakes:
Using POST for delete operations
Ignoring HTTP method semantics
Assuming POST is idempotent
5. You want to design an API endpoint to update user profile data safely. Which approach ensures idempotency and avoids duplicate updates if the client retries the request multiple times?
hard
A. Use PUT method to update the entire user profile with the same data each time
B. Use GET method with query parameters to update user data
C. Use PATCH method to partially update user data without tracking changes
D. Use POST method to update user data with a unique request ID
Solution
Step 1: Understand idempotent update methods
PUT replaces the entire resource and is idempotent, so repeated calls with the same data do not cause duplicate changes.
Step 2: Evaluate options for safe updates
POST is not idempotent, PATCH is not guaranteed idempotent, GET should not modify data. PUT is the best choice for safe, repeatable updates.
Final Answer:
Use PUT method to update the entire user profile with the same data each time -> Option A
Quick Check:
PUT ensures idempotent updates = D [OK]
Hint: PUT replaces resource fully, safe for retries [OK]
Common Mistakes:
Using POST for updates expecting idempotency
Assuming PATCH is always idempotent
Trying to update data with GET
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.