0
0
Rest APIprogramming~20 mins

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

Choose your learning style9 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.