Middle
Tell me about idempotency in HTTP. Which methods are idempotent and what do they do?
sobes.tech AI
Answer from AI
Idempotence in HTTP means that performing the same request multiple times results in the same server state, without additional side effects.
Idempotent HTTP methods:
- GET — requests data without changing the server state. Repeated GET requests return the same result.
- PUT — updates or creates a resource at the specified URI. Repeated PUT requests with the same data do not change the result after the first.
- DELETE — deletes a resource. Repeated DELETE requests to the same resource after its deletion do not change the state.
- HEAD — like GET but without the response body, and is idempotent.
- OPTIONS — returns supported methods, and is idempotent.
Non-idempotent method:
- POST — usually creates resources or initiates operations with side effects. Repeated POST requests may create duplicates or alter the state.
Example:
PUT /users/123 HTTP/1.1
Content-Type: application/json
{"name": "Alice"}
Repeating this request will update the user with id 123 identically, without creating new records.
Idempotence is important for reliability and repeated execution of requests without the risk of data inconsistency.