Skip to content

If-None-Match

Description

The If-None-Match request header is an HTTP conditional request header used to determine if a resource has changed on the server before fetching or modifying it. It is primarily used for caching and concurrency control.

When a client (such as a web browser) makes a request to a server, it may include an If-None-Match header with an entity tag (ETag) value that was previously returned by the server. The server then compares the provided ETag value with the current ETag of the requested resource:

  • If the ETag matches, the server responds with a 304 Not Modified status, preventing unnecessary data transfer.
  • If the ETag does not match, the server responds with the requested resource, allowing the client to update its cache.

This header is particularly useful for optimizing web application performance by reducing bandwidth usage and ensuring that clients do not receive redundant data.

Syntax

The If-None-Match header is used in an HTTP request as follows:

If-None-Match: "etag_value"

It can also accept multiple ETag values separated by commas:

If-None-Match: "etag1", "etag2", "etag3"

Alternatively, the wildcard (*) can be used to check if any version of the resource exists:

If-None-Match: *

Examples

Conditional GET Request

A client requesting a resource conditionally using If-None-Match:

Client Request:

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

Server Response (If ETag Matches):

HTTP/1.1 304 Not Modified
Date: Mon, 18 Feb 2025 12:00:00 GMT

Server Response (If ETag Does Not Match):

HTTP/1.1 200 OK
Content-Type: application/json
ETag: "def456"

{"data": "updated content"}

Conditional PUT Request

A client updating a resource only if it has not changed:

Client Request:

PUT /resource HTTP/1.1
Host: example.com
If-None-Match: "abc123"
Content-Type: application/json

{"data": "new content"}

Server Response (If ETag Matches - Update Denied):

HTTP/1.1 412 Precondition Failed

Server Response (If ETag Does Not Match - Update Allowed):

HTTP/1.1 200 OK
ETag: "ghi789"

Checking for Any Version of a Resource

A client checking if any version of a resource exists before creating it:

Client Request:

PUT /resource HTTP/1.1
Host: example.com
If-None-Match: *
Content-Type: application/json

{"data": "new resource"}

Server Response (If Resource Exists - Creation Denied):

HTTP/1.1 412 Precondition Failed

Server Response (If Resource Does Not Exist - Creation Allowed):

HTTP/1.1 201 Created
ETag: "xyz123"

Summary

The If-None-Match request header is a powerful tool for optimizing web performance and ensuring data consistency. It helps prevent redundant downloads, minimizes bandwidth usage, and ensures updates are only applied when necessary. By leveraging ETags, clients can make smarter requests, reducing unnecessary server load and improving overall efficiency. This header plays a crucial role in modern web caching and concurrency strategies, making it an essential component for web developers and API designers.