Understanding ETag and If-Match
Contents
REST API request and response headers sometimes include fields called ETag and If-Match.
In this post, I go over ETag and If-Match, which are standard HTTP mechanisms:
- What ETag and If-Match are
- How they prevent "accidental overwrites" (optimistic locking / optimistic concurrency control)
- Does adding If-Match cause a CORS error?
| Header | Who sends it? | Role |
|---|---|---|
ETag |
Server → client | An identifier for the resource's "state (version)" |
If-Match |
Client → server | A condition: "update it only if it still has this ETag" |
Normally, ETag is used as a response header and If-Match as a request header.
As an example, consider a case where "users A and B edit the same data".
- User A sends an update based on old content
- Meanwhile, user B had already updated it first
- A overwrites B's update without knowing about it
ETag + If-Match prevents this accident.
GET /items/123 HTTP/1.1
Accept: application/json
HTTP/1.1 200 OK
Content-Type: application/json
ETag: "v3"
{
"id": 123,
"name": "Item A"
}
PATCH /items/123 HTTP/1.1
Content-Type: application/json
If-Match: "v3"
{
"name": "Item A Updated"
}
- If the latest ETag on the server is still "v3", the update is OK
- If it has already changed to something like "v4", the update is rejected (preventing an accidental overwrite)
HTTP/1.1 204 No Content
ETag: "v4"
The ETag usually advances or changes after an update, so it's helpful to return it.
When someone else updated it first and the ETag changed:
HTTP/1.1 412 Precondition Failed
Content-Type: application/json
{
"message": "Resource has been modified. Fetch latest and retry."
}
Because the server can return this 412, the client can respond safely, for example by:
- fetching the latest version again
- showing a UI for merging the differences
- notifying the user that "it was updated elsewhere"
Because If-Match is added to the request headers, it can result in a CORS error.
Preflight example:
OPTIONS /items/123 HTTP/1.1
Origin: https://app.example.com
Access-Control-Request-Method: PATCH
Access-Control-Request-Headers: if-match, content-type
If the server doesn't respond to this with the headers it allows, the browser blocks the request and you get a CORS error.
Example:
Access-Control-Allow-Headers: Content-Type, Authorization, If-Match
Note
If-Match itself doesn't "cause" the CORS error. For cross-origin requests, the browser uses the preflight to check the allow list of headers it may send. If that allow list (Access-Control-Allow-Headers) doesn't include If-Match, the browser blocks the request and you get a CORS error.
Just "making it possible to send If-Match" sometimes isn't enough: the frontend may not be able to read the response's ETag. (For example, you can see it in the developer tools, but JavaScript can't read it.)
In general, the response headers JavaScript can access are limited by default to the CORS-safelisted response headers, and ETag isn't one of them.
So in a cross-origin request, trying to get it with something like res.headers.get("ETag") returns null.
To let browser JS read the ETag, the server has to expose it explicitly with Access-Control-Expose-Headers.
Access-Control-Expose-Headers: X-Custom-Header, ETag
https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/ETag https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/If-Match https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Access-Control-Allow-Headers https://developer.mozilla.org/en-US/docs/Glossary/CORS-safelisted_response_header
Translated from the Japanese original.
Thanks. I'll look into it.
Sorry, that didn't go through. Please try again later.