Skip to content

ETag

Description

The ETag (Entity Tag) response header is a unique identifier assigned to a specific version of a resource. It allows clients to make conditional requests that can be more efficient than regular requests. ETags provide a mechanism for web caches to validate whether a resource has changed, without having to download the entire resource again.

ETags serve a similar purpose to the Last-Modified header but can be more precise because they're based on the content itself rather than just the modification time. This makes ETags particularly valuable when resources change frequently or when the modification time cannot reliably indicate content changes.

There are two types of ETags: - Strong ETags: Guarantee byte-for-byte identity for a resource. - Weak ETags: Indicate semantic equivalence but not necessarily byte-for-byte identity.

Syntax

The syntax of the ETag header follows this structure:

ETag: [W/]"<etag-value>"
  • W/: Optional prefix indicating a weak ETag.
  • "<etag-value>": A quoted string representing the entity tag value, typically a hash of the content.

Example Syntax

ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"

This example represents a strong ETag.

ETag: W/"0815"

This example represents a weak ETag.

Examples

Basic Example

A response with an ETag header:

HTTP/1.1 200 OK
Date: Mon, 02 Jun 2025 10:00:00 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 1234
ETag: "abc123def456"

<!DOCTYPE html>
<html>
<head><title>Example Page</title></head>
<body><p>Content with ETag "abc123def456"</p></body>
</html>

Conditional GET Example

A client making a conditional request with an ETag:

GET /resource HTTP/1.1
Host: example.com
If-None-Match: "abc123def456"

If the resource hasn't changed, the server responds with:

HTTP/1.1 304 Not Modified
Date: Mon, 02 Jun 2025 10:10:30 GMT
ETag: "abc123def456"
Cache-Control: max-age=3600

If the resource has changed, the server responds with the new content and a new ETag:

HTTP/1.1 200 OK
Date: Mon, 02 Jun 2025 10:10:30 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 1345
ETag: "789ghi101112"

<!DOCTYPE html>
<html>
<head><title>Updated Example Page</title></head>
<body><p>Updated content with ETag "789ghi101112"</p></body>
</html>

Conditional PUT Example

A client trying to update a resource without causing a conflict:

PUT /resource HTTP/1.1
Host: example.com
If-Match: "abc123def456"
Content-Type: text/html
Content-Length: 1456

<!DOCTYPE html>
<html>
<head><title>Updated by Client</title></head>
<body><p>Updated content from client</p></body>
</html>

If the current ETag matches, the server updates the resource:

HTTP/1.1 200 OK
Date: Mon, 02 Jun 2025 10:20:45 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
ETag: "131415jkl161718"

If the ETag doesn't match (meaning someone else modified the resource), the server returns an error:

HTTP/1.1 412 Precondition Failed
Date: Mon, 02 Jun 2025 10:20:45 GMT
Content-Type: text/plain
Content-Length: 95

The resource has been modified by another request. Please retrieve the latest version and try again.

Summary

The ETag response header provides a powerful mechanism for optimizing web performance through conditional requests. By assigning a unique identifier to each version of a resource, servers enable clients to determine whether their cached copy is still valid without downloading the entire resource again. This reduces bandwidth usage, server load, and improves response times. ETags are particularly valuable for resources that change frequently or unpredictably, and they can be used in combination with conditional request headers like If-Match and If-None-Match to implement efficient caching and concurrency control in web applications.