Bird
Raised Fist0
Rest APIprogramming~20 mins

Idempotency of methods in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Idempotency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Idempotency in HTTP Methods

Which HTTP method is idempotent by definition, meaning that making the same request multiple times will have the same effect as making it once?

ACONNECT
BGET
CPOST
DTRACE
Attempts:
2 left
💡 Hint

Think about methods that retrieve data without changing server state.

❓ Predict Output
intermediate
2:00remaining
Idempotency of PUT vs POST

Consider a REST API where you update a user profile with PUT and create a new user with POST. What is the output of the following sequence of requests?

1. PUT /users/123 with data {"name": "Alice"}
2. PUT /users/123 with data {"name": "Alice"}
3. POST /users with data {"name": "Alice"}

Which statement is true about the server state after these requests?

APOST is idempotent and does not create new users on repeated calls.
BEach PUT creates a new user; POST updates the user with ID 123.
CThe server state changes after each PUT and POST; none are idempotent.
DThe user with ID 123 is updated twice; a new user is created once. The state after step 2 and step 1 is the same.
Attempts:
2 left
💡 Hint

Remember that PUT replaces or creates a resource at a known URL, while POST creates new resources.

🔧 Debug
advanced
2:00remaining
Identifying Non-Idempotent Behavior

Given this pseudo-code for a REST API endpoint:

def delete_user(user_id):
    if user_exists(user_id):
        remove_user(user_id)
        return "Deleted"
    else:
        return "User not found"

What happens if a client sends the DELETE request twice for the same user_id?

AFirst call deletes the user; second call returns "User not found". DELETE is idempotent.
BBoth calls delete the user again, causing an error.
CSecond call deletes the user twice, causing data corruption.
DBoth calls return "Deleted" even if the user does not exist.
Attempts:
2 left
💡 Hint

Idempotent methods can be called multiple times without changing the result beyond the initial call.

📝 Syntax
advanced
1:30remaining
Correct Idempotent HTTP Method Usage

Which of the following HTTP request examples correctly uses an idempotent method to update a resource?

APUT /items/42 with body {"id": 42, "price": 10.99}
BPOST /items with body {"id": 42, "price": 10.99}
CDELETE /items/42
DPATCH /items/42 with body {"price": 10.99}
Attempts:
2 left
💡 Hint

PUT replaces the entire resource and is idempotent; PATCH applies partial updates and is not guaranteed idempotent.

🚀 Application
expert
2:30remaining
Designing an Idempotent API Endpoint

You are designing a REST API endpoint to process payment transactions. Which approach ensures the endpoint is idempotent?

AUse GET /payments to create payments to ensure idempotency.
BUse POST /payments without any transaction ID and process every request as new.
CUse POST /payments with a unique client-generated transaction ID in the request body and reject duplicates.
DUse DELETE /payments/{payment_id} to create payments and ensure idempotency.
Attempts:
2 left
💡 Hint

Idempotency in payment processing often requires client-generated unique IDs to avoid duplicate charges.

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

  1. 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.
  2. 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.
  3. Final Answer:

    PUT -> Option C
  4. 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

  1. Step 1: Identify the HTTP method for deletion

    DELETE is the HTTP method used to remove a resource and is idempotent.
  2. Step 2: Check curl syntax for DELETE

    The correct curl command uses -X DELETE followed by the resource URL.
  3. Final Answer:

    curl -X DELETE https://api.example.com/resource/123 -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    User's email is updated once and remains the same after second request -> Option B
  4. 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:
fetch('https://api.example.com/items/10', { method: 'POST' })
  .then(response => console.log('Deleted'));

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

  1. Step 1: Identify the HTTP method used

    The code uses POST to delete a resource, which is incorrect because POST is not idempotent.
  2. Step 2: Understand correct method for deletion

    DELETE method should be used for idempotent deletion, ensuring repeated calls do not cause unintended effects.
  3. Final Answer:

    POST is not idempotent and should not be used for deletion -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Use PUT method to update the entire user profile with the same data each time -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard