Skip to content
On this page

If-Match

Description

The If-Match request header is used in HTTP requests to make the request conditional. It ensures that a request, such as an update (PUT) or deletion (DELETE), is only performed if the specified entity tag (ETag) matches the current entity on the server. This helps prevent unintentional overwrites caused by concurrent modifications.

This header is commonly used in optimistic concurrency control mechanisms to ensure that an update occurs only if the resource has not changed since the client last accessed it.

Syntax

The syntax of the If-Match header follows this structure:

plaintext
If-Match: <ETag>
If-Match: *
  • <ETag>: A quoted entity tag that must match the current entity on the server.
  • *: Indicates that the request should be processed if the resource exists, regardless of its current ETag value.

Example Syntax

plaintext
If-Match: "abc123"
If-Match: *

Examples

Updating a Resource with ETag Verification

A client retrieves a resource and its ETag before updating it:

plaintext
GET /resource HTTP/1.1
Host: example.com

Server response:

plaintext
HTTP/1.1 200 OK
ETag: "xyz789"

<resource content>

The client then attempts to update the resource using If-Match:

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

{"updated": "data"}

If the ETag matches, the server allows the update:

plaintext
HTTP/1.1 200 OK

If the resource has changed and the ETag no longer matches, the server rejects the update:

plaintext
HTTP/1.1 412 Precondition Failed

Preventing Accidental Deletion

A client wants to delete a resource only if it hasn’t changed:

plaintext
DELETE /data HTTP/1.1
Host: example.com
If-Match: "abc123"

If the ETag matches, the server deletes the resource. Otherwise, it returns a 412 Precondition Failed status.

Summary

The If-Match request header ensures that operations like updates and deletions are performed only if a resource matches a specific ETag, preventing conflicts in concurrent environments. It is a key component of optimistic concurrency control, helping maintain data integrity and preventing accidental overwrites.

Released under the MIT License.