Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1105799 of ETag

  • Revision slug: Web/HTTP/Headers/ETag
  • Revision title: ETag
  • Revision id: 1105799
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

{{HTTPSidebar}}

The ETag HTTP response header is an identifier for a specific version of a resource. It allows caches to be more efficient, and saves bandwidth, as a web server does not need to send a full response if the content has not changed. On the other side, If the content has changed, Etags can be useful to prevent simultaneous updates of a resource from overwriting each other ("mid-air collisions").

If the resource at a given URL changes, a new Etag should be generated. Etags are therefore similar to fingerprints. A comparison of them allows to quickly determine whether two representations of a resource are the same.

Header type {{Glossary("Response header")}}
{{Glossary("Forbidden header name")}} no

Syntax

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

Directives

"W/" {{optional_inline}}
"W/" (case-sensitive) indicates that a weak validator is used. Weak validators 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 Etags of two representations might be semantically equivalent, but not byte-for-byte identical.
"<etag_value>"
Entity tags uniquely representing the requested resources. They are a string of ASCII characters placed between double quotes (Like "675af34563dc-tr34"). The method by which ETag values are generated is not specified. Oftentimes, a hash of the content, a hash of the last modification timestamp, or just a revision number is used. MDN uses a hash of hexadecimal digits of the wiki content, for example.

Examples

ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"
ETag: "W/" "0815"

Avoiding mid-air collisions

With the help of the ETag and the {{HTTPHeader("If-Match")}} headers, you are able to detect mid-air edit collisions.

For example when editing MDN, the current wiki content is hashed and put into an Etag in the response:

ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4

When saving changes to a wiki page (posting data), the {{HTTPMethod("POST")}} request will contain the {{HTTPHeader("If-Match")}} header containing the ETag values to check freshness against.

If-Match: "33a64df551425fcc55e4d42a148795d9f25f89d4"

If the hashes don't match, it means that the document has been edited in-between and a {{HTTPStatus("412")}} Precondition Failed error will be thrown.

Caching of unchanged resources

Another typical use case of the ETag header is to cache resources that are unchanged. If a user visits a given URL again (that has an ETag set), the client will send the value of its ETag along in an {{HTTPHeader("If-None-Match")}} header field:

If-None-Match: "33a64df551425fcc55e4d42a148795d9f25f89d4"

If the server now compares the client's ETag (sent with If-None-Match) with the ETag for the current version of the resource and both values match (that is, the resource has not changed), the server can send back a {{HTTPStatus("304")}} Not Modified status, which tells the client that the cached version of the response is still good to use.

Specifications

Specification Title
{{RFC("7232", "ETag", "2.3")}} Hypertext Transfer Protocol (HTTP/1.1): Conditional Requests

Browser compatibility

{{Compat}}

See also

Revision Source

<div>{{HTTPSidebar}}</div>

<p>The <strong><code>ETag</code></strong> HTTP response header is an identifier for a specific version of a resource. It allows caches to be more efficient, and saves bandwidth, as a web server does not need to send a full response if the content has not changed. On the other side, If the content has changed, Etags can be useful to prevent simultaneous updates of a resource from overwriting each other ("mid-air collisions").</p>

<p>If the resource at a given URL changes, a new Etag should be generated. Etags are therefore similar to fingerprints. A comparison of them allows to quickly determine whether two representations of a resource are the same.</p>

<table class="properties">
 <tbody>
  <tr>
   <th scope="row">Header type</th>
   <td>{{Glossary("Response header")}}</td>
  </tr>
  <tr>
   <th scope="row">{{Glossary("Forbidden header name")}}</th>
   <td>no</td>
  </tr>
 </tbody>
</table>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
ETag: "W/" "&lt;etag_value&gt;"
ETag: "&lt;etag_value&gt;"
</pre>

<h2 id="Directives">Directives</h2>

<dl>
 <dt>"W/" {{optional_inline}}</dt>
 <dd>"W/" (case-sensitive) indicates that a weak validator is used. Weak validators 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 Etags of two representations might be semantically equivalent, but not byte-for-byte identical.</dd>
 <dt>"&lt;etag_value&gt;"</dt>
 <dd>Entity tags uniquely representing the requested resources. They are a string of ASCII characters placed between double quotes (Like <code>"675af34563dc-tr34"</code>). The method by which ETag values are generated is not specified. Oftentimes, a hash of the content, a hash of the last modification timestamp, or just a revision number is used. MDN uses a hash of hexadecimal digits of the wiki content, for example.</dd>
</dl>

<h2 id="Examples">Examples</h2>

<pre>
ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"
ETag: "W/" "0815"</pre>

<h3 id="Avoiding_mid-air_collisions">Avoiding mid-air collisions</h3>

<p>With the help of the <code>ETag</code> and the {{HTTPHeader("If-Match")}} headers, you are able to detect mid-air edit collisions.</p>

<p>For example when editing MDN, the current wiki content is hashed and put into an <code>Etag</code> in the response:</p>

<pre>
ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4</pre>

<p>When saving changes to a wiki page (posting data), the {{HTTPMethod("POST")}} request will contain the {{HTTPHeader("If-Match")}} header containing the <code>ETag</code> values to check freshness against.</p>

<pre>
If-Match: "33a64df551425fcc55e4d42a148795d9f25f89d4"</pre>

<p>If the hashes don't match, it means that the document has been edited in-between and a {{HTTPStatus("412")}}<code> Precondition Failed</code> error will be thrown.</p>

<h3>Caching of unchanged resources</h3>

<p>Another typical use case of the <code>ETag</code> header is to cache resources that are unchanged. If a user visits a given URL again (that has an <code>ETag</code> set), the client will send the value of its <code>ETag</code> along in an {{HTTPHeader("If-None-Match")}} header field:</p>

<pre>
If-None-Match: "33a64df551425fcc55e4d42a148795d9f25f89d4"</pre>

<p>If the server now compares the client's <code>ETag</code> (sent with <code>If-None-Match</code>) with the <code>ETag</code> for the current version of the resource and both values match (that is, the resource has not changed), the server can send back a {{HTTPStatus("304")}}<code> Not Modified</code> status, which tells the client that the cached version of the response is still good to use.</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Title</th>
  </tr>
  <tr>
   <td>{{RFC("7232", "ETag", "2.3")}}</td>
   <td>Hypertext Transfer Protocol (HTTP/1.1): Conditional Requests</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p class="hidden">The compatibility table in this page is generated from structured data. If you’d like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data/README.md">https://github.com/mdn/browser-compat-data/README.md</a> and send us a pull request.</p>

<p>{{Compat}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{HTTPHeader("If-Match")}}</li>
 <li>{{HTTPHeader("If-None-Match")}}</li>
 <li>{{HTTPStatus("304")}}<code> Not Modified</code></li>
 <li>{{HTTPStatus("412")}}<code> Precondition Failed</code></li>
 <li>
  <p><a href="https://www.w3.org/1999/04/Editing/">W3C Note: Editing the Web – Detecting the Lost Update Problem Using Unreserved Checkout</a></p>
 </li>
</ul>
Revert to this revision