Adding ETags to API responses
An ETag is an HTTP response header that contains a computed hash or equivalent value of the response entity, and a minor change in the entity must change its value. HTTP request objects can then contain the If-None-Match
and If-Match
headers to receive the conditional responses.
Let’s call an API to retrieve the response with an ETag, as shown next:
$ curl -v --location --request GET 'http://localhost:8080/ api/v1/products/6d62d909-f957-430e-8689-b5129c0bb75e' –-header 'Content-Type: application/json' --header 'Accept: application/json'* … text trimmed > GET /api/v1/products/6d62d909-f957-430e-8689-b5129c0bb75e HTTP/1.1 > Host: localhost:8080 > User-Agent: curl/7.55.1 > Content-Type: application/json > Accept: application/json > < HTTP/1.1 200 < ETag: "098e97de3b61db55286f5f2812785116f" < Content-Type: application/json < Content-Length: 339 < { ...