How to use Etag for REST API?

What is Etag?

Etag, also known as entity tag is an HTTP response header. Etag is used as a version identifier of the resource and will be available in HTTP Response. To handle concurrency situations, you can pass Etag in your HTTP request header. There are two main uses of for Etags:

  • Conditional GET request
  • Concurrency Control

1. Conditional GET request

Conditional GET request helps in the caching and reduce the data traffic. Conditional GETs request allow a client to ask a server if a resource has changed. If it has not changed, it can assume it’s current knowledge is up to date. If it has changed, the server will send the resource back to the client.

2. Concurrency Control

Concurrency control is an important aspect if we have multiple REST clients. These clients can update the same data at the same time leading to data inconsistencies.

How to check Etag value?

Step 1
When you get the REST API response, you can expand the metadata in results and find out one parameter called Etag.

Step 2
If you are getting only one element or multiple element using a GET request, you will be able to get Etag using the code.

Syntax

ETag: W/"<etag_value>"
ETag: "<etag_value>"

Example

ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"  (strong validation)
ETag: W/"0815"                                    (weak validation)

Directives

1. W/ Optional

‘W/’ It is case-sensitive. This is symbolic of weak validation. It indicates that a weak validator is used. Weak etags are easy to generate, but are far less useful for comparisons. Strong validators are ideal for comparisons but can be very difficult to generate efficiently. Weak ETag values of two representations of the same resources might be semantically equivalent, but not byte-for-byte identical. This means weak etags prevent caching when byte range requests are used, but strong etags mean range requests can still be cached.

2. “<etag_value>”

Entity tag that uniquely represents the requested resource. It is a string of ASCII characters placed between double quotes, like “675af34563dc-tr34”. The method by which ETag values are generated is not specified. The ETag value is a hash of the content, a hash of the last modification timestamp, or just a revision number. For example, a wiki engine can use a hexadecimal hash of the documentation article content.

Validation of ETag value

It is divided into 2 parts.

  • Strong Validation
  • Weak Validation

1. Strong Validation

The different resource representations are byte-for-byte identical. This is the default validation of ETag and no special directive is used for it.

2. Weak Validation

The two resource representations are semantically equivalent. For e.g. the current date displayed on the page might not be important for updating the entire resource for it.

Working of ETag with Request Headers

An ETag  is an HTTP response header returned by an HTTP/1.1 compliant web server used to determine change in content at a given URL. ETags can use for two things – caching and conditional requests.

The ETag value can be thought of as a hash computed out of the bytes of the Response body. Because the service likely uses a cryptographic hash function, even the smallest modification of the body will drastically change the output and thus the value of the ETag. This is only true for strong ETags – the protocol does provide a weak Etag as well.

Working of Etag with request headers – If-Match Header & If-None-Match Header

1. If-Match Header

This is primarily used when multiple agents might be working on the same resource , thereby to prevent accidental overwrites while using methods such as POST, PUT, DELETE etc.

2. If-None-Match Header

If-None-Match Header is used when the already stored response of a specific website previously visited by the user has expired.

Start Integrating APITier API!!