</>

Technology

Rest Assured

Difficulty

Intermediate

Interview Question

What is idempotency in API and how does it apply to HTTP methods?

Answer

Idempotency in API

Idempotency refers to the property of an operation where applying the operation multiple times has the same effect as applying it once.

In API testing, idempotent methods ensure that performing the same request multiple times will not produce different outcomes beyond the initial request''s effect.

Idempotency by HTTP Method

MethodIdempotentExample
GETYesRetrieving user info — same result every time
POSTNoCreating a user — creates a new record each time
PUTYesUpdating user info — same final state regardless
PATCHNoPartial update — result may vary based on current state
DELETEYesDeleting a user — resource deleted, subsequent calls have no effect

Examples

GET — Idempotent: Yes

CODE
GET /api/users/123

Returns the same user data regardless of how many times called.

POST — Idempotent: No

CODE
POST /api/users
{ "name": "John" }

Each call creates a NEW user profile with a different ID.

PUT — Idempotent: Yes

CODE
PUT /api/users/123
{ "name": "Updated John" }

Repeating this always results in the same final state.

DELETE — Idempotent: Yes

CODE
DELETE /api/users/123

First call deletes the user. Subsequent calls return 404 — no additional effect.

Why This Matters

In case of network failures during API requests, idempotent methods ensure that retrying the request does not lead to unintended consequences such as duplicate resource creation or data corruption.

Follow AutomateQA

Related Topics