HTTP headers | ETag
ETag or Entity Tag is a response-type header that works as a validator to let client make conditional requests. It makes re-validation requests more efficient by triggering request headers which help with web cache validation that makes economical use of network bandwidth.
ETag is generated as identification for specific browser resources. Each time the user opens the same resource, the browser sends a small token to verify if this version of the resource already present in the browser’s HTTP cache matches with the current version on the webserver. If the resource matches then web server need not send a full response. This makes process faster and saves data.
ETag : "etag-value" (strong validation) ETag : W/"etag-value" (weak validation)
Directives : This header accepts two directive as mentioned above and described below:
- “etag-value”: It is a string of ASCII characters in double-quotes. A new value is generated every time the browser resource changes and it is unique. There is no HTTP specification about the order of value generated. Therefore , the method of value generation is completely dependent on the webserver.
- W/ : This is symbolic of weak validation. They are very easy to generate compared to strong validator tags but differ in performing a comparison process. A weak comparison considers two tags equivalent if their opaque-tags match character-by-character, irrespective either or both being tagged as “weak”. While a strong comparison considers two tags equivalent only if both match character by character and both tags are not weak.
Working of ETag with Request Headers :
- 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. Here, a ETag header value for a particular resource is contained within an If-Match header to check if while performing the specified action the resource undergoes any change or not. 412 Precondition Failed error is shown if the match does not occur. It can also be used to abort a request if the current representation does not match the prior request completely or partially.
ETag : "21e92a357b3434b5aa" If-Match : "21e92a357b3434b5aa"
- If-None-Match Header : This header is used when the already stored response of a specific website previously visited by the user has expired. Here , server generates If-None-Match value for the current version and compares it with the ETag value stored in user’s browser. A 304 Not Modified status code is shown if both values match. Weak comparison must be used in this scenario while comparing entity tags.
If-None-Match : "21e92a357b3434b5aa"
ETag: "21e92a357b3434b5aa" (strong validation) ETag: W/"21e92a457b3434b5aa" (weak validation)
Supported Browsers : The browsers are supported by HTTP header | ETag header are listed below:
- Microsoft Edge
- Google Chrome
- Mozilla Firefox
- Internet Explorer