Open In App

What is HTTP ETag?

Last Updated : 29 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

ETag :

An entity tag (ETag) is an HTTP header used for Web cache validation and conditional request from browsers to resources. The value of an ETag is an identifier that represents a specific version of the resource. Additionally, ETags help prevents simultaneous updates of a resource from overwriting each other. Example of ETag header is

ETag: "version1"

Note :The value of the ETag header must be in double-quotes.

Working of ETag

  • The server receives an HTTP request for a particular resource.
  • The server generates a response and attached an ETag header. For Eg: ETag: “response_version1”.
  • The server sends the response with the above header with the status code 200. Then the application represents the resource and at the same time caches the resource copy along with header information.
  • Later the same application makes another request for the same resource with following conditional request header: If-None-Match: “response_version1”
  • On receiving the request for the resource along with the ‘If-None-Match’ header, the server-side logic compare the current value of the ETag identifier on the server-side and the one which is received in the request header.
  • If the request’s If-None-Match is the same as the currently generated value of ETag on the server, then status code 304 (Not Modified) with the empty body is sent back and the application uses the cached copy of the resource.
  • If the request’s If-None-Match value doesn’t match the currently generated/assigned value of ETag (say “response_version2”) for the same resource then the server sends back the new content in the body along with status code 200. The ‘ETag’ header with the new value is also included in the response. The application uses the new resource and updates its cache with the new data.

Generating ETag Value

That’s entirely up to the application to generate it as it wants. It can be created and updated manually or can be auto-generated. Common methods of its auto-generation include using a hash of the resource’s content or just a hash of the last modification timestamp. The generated hash should be collision-free.

Validation of ETag value

It is just comparison of two values. It is divided into 2 parts

  1. Strong Validation
  2. 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.

References:

  1. https://en.wikipedia.org/wiki/HTTP_ETag
  2. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads