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:
<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
Examples
Updating a Resource with ETag Verification
A client retrieves a resource and its ETag before updating it:
Server response:
The client then attempts to update the resource using If-Match
:
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:
If the resource has changed and the ETag no longer matches, the server rejects the update:
Preventing Accidental Deletion
A client wants to delete a resource only if it hasn’t changed:
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.