What is HTTP ETag?

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jyash97

Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.