Middle
How to implement an idempotent POST so that it is not semantically identical to PUT?
sobes.tech AI
Answer from AI
Idempotency of POST can be implemented by using a unique request identifier (for example, Idempotency-Key), which the client generates and sends along with the request. The server stores this key and the result of processing the request. If a subsequent request with the same key arrives, the server returns the same response without creating a new resource.
This differs from PUT, which semantically updates or creates a resource at a known URI, whereas POST typically creates a resource with a server-generated identifier.
Example:
- The client sends a POST with
Idempotency-Key: 12345to create an order. - The server creates the order and saves the result with the key 12345.
- On a subsequent request with the same key, the server returns the already created order without creating a new one.
Thus, POST remains semantically a resource creation, but becomes idempotent through the key, and does not turn into PUT, which updates a resource at a known URI.