This example demonstrates basic idempotency handling with Gin Gonic using in-memory storage.
cd examples/gin-basic
go run main.gocurl -X POST http://localhost:8080/payment \
-H 'Content-Type: application/json' \
-H 'Idempotency-Key: payment-001' \
-d '{"amount":100.50,"currency":"USD"}'Response:
{
"status": "success",
"amount": 100.50,
"currency": "USD",
"timestamp": 1708123456
}Same request with same idempotency key:
curl -X POST http://localhost:8080/payment \
-H 'Content-Type: application/json' \
-H 'Idempotency-Key: payment-001' \
-d '{"amount":100.50,"currency":"USD"}'Response (with X-Idempotent-Replayed: true header):
{
"status": "success",
"amount": 100.50,
"currency": "USD",
"timestamp": 1708123456
}Note: The timestamp is the same as the first request!
curl -X POST http://localhost:8080/payment \
-H 'Content-Type: application/json' \
-H 'Idempotency-Key: payment-001' \
-d '{"amount":200.00,"currency":"EUR"}'Response:
{
"error": "request with same idempotency key has different content"
}Status Code: 422 Unprocessable Entity
- ✅ Request deduplication
- ✅ Response caching
- ✅ Request validation (body mismatch detection)
- ✅ In-memory storage
- ✅ Multiple endpoints with idempotency