Are all HTTP methods idempotent?
sobes.tech AI
Answer from AI
No, not all HTTP methods are idempotent.
Idempotency means that if you perform the same request multiple times with the same parameters, the result on the server side will not change after the first successful execution.
Main HTTP methods and their idempotency:
| Method | Idempotent | Purpose |
|---|---|---|
GET |
Yes | Retrieve data |
HEAD |
Yes | Retrieve response headers |
PUT |
Yes | Create or completely replace a resource |
DELETE |
Yes | Delete a resource |
POST |
No | Send data to create a resource or perform an action |
PATCH |
No | Partial update of a resource |
POST and PATCH are not idempotent because each execution can lead to creating a new resource or changing the resource's state depending on the implementation, which is not the same after the first successful execution. For example, multiple executions of a POST request to create a user will usually result in multiple users being created.
Example of a non-idempotent POST:
{
"username": "testuser"
}
Sending such a POST request can create a new user with username: "testuser" each time.
Example of an idempotent PUT:
{
"id": 123,
"status": "completed"
}
Sending such a PUT request to the resource with ID 123 will set its status to "completed". Repeating it will not change the status if it is already "completed".