HTTP Headers
ETag
The HTTP ETag
(entity tag) response header is an identifier for a specific version of a resource.
It allows caches to operate more efficiently and conserve bandwidth since a web server does not need to resend the full response if the content remains unchanged.
Furthermore, ETags are instrumental in preventing conflicting updates to a resource, commonly referred to as “mid-air collisions”.
Whenever the content at a particular URL is modified, a new ETag
value must be generated.
By comparing ETag values, it can be determined whether two different representations of a resource are identical.
Syntax
ETag: W/"<etag_value>"
ETag: "<etag_value>"
Directives
W/
OptionalW/
(case-sensitive) indicates that a weak validator is used.
Weak ETags are easy to generate but are less reliable for comparison purposes.
Strong validators provide more accuracy for comparisons but are more complex to generate efficiently.
Weak ETags of different representations of the same resource may be semantically equivalent but not byte-for-byte identical.
This difference means weak ETags allow caching when byte range requests are involved, whereas strong ETags support caching for range requests.<etag_value>
An entity tag that uniquely identifies a specific version of the requested resource. It is typically a string of ASCII characters enclosed in double quotes, such as
"675af34563dc-tr34"
.
The exact method of generatingETag
values is generally not specified, but it is often a hash of the content, the last modification timestamp, or a revision number.
For example, a wiki engine might use a hexadecimal hash derived from the content of an article to generate its ETag.
Example
ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"
ETag: W/"0815"
With the help of the ETag
and the If-Match
headers, you can detect mid-air edit collisions (conflicts).
For example, when editing a wiki, the current wiki content may be hashed and put into an Etag
header in the response:
ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"
When saving changes to a wiki page (posting data), the POST
request will contain an If-Match
header with the ETag
values to check for freshness.
Example:
If-Match: "33a64df551425fcc55e4d42a148795d9f25f89d4"
If the hashes don’t match, it indicates that the document was edited in the meantime and a 412 Precondition Failed
error is returned.
Another common use of the ETag
header is to cache resources that haven’t changed. If a user revisits a URL with an ETag
set and the resource is stale, the client sends its ETag
in an If-None-Match
header:
Example:
If-None-Match: "33a64df551425fcc55e4d42a148795d9f25f89d4"
The server compares this value with the current ETag
of the resource. If they match, meaning the resource hasn’t changed, the server responds with a 304 Not Modified
status, indicating that the cached version is still valid and can be used.
How to Modify Header using Requestly
Requestly is a powerful Chrome extension that allows you to modify HTTP headers, including the ETag header. This is especially useful for testing how your application handles caching and resource validation during development or debugging. Steps to Modify the ETag Header:
- Install and open the Requestly Chrome extension. You can find it on the Chrome Web Store.
- Create a new rule: Click on “Create Rule” and choose “Modify Headers” from the list of available rule types.
- Add a new header modification:
- Under “Action”, select “Add” or “Override”.
- In the “Header Name” field, enter ETag.
- In the “Header Value” field, enter your desired ETag value (e.g., “123456789”).
- Set the URL condition: Specify the URL or pattern where this header change should apply (e.g., https://your-api.com/*).
- Save the rule.
Once set up, Requestly will inject the specified ETag header into all matching requests, helping you test how your application handles caching and resource validation based on ETag values.
Modifying the ETag header is helpful because ETags are used by browsers and servers to determine if a resource has changed. By changing this header, you can test how your app reacts to updates or cached content, ensuring your caching strategy works correctly and improving overall performance.
Table of Contents
- No headings found.