How do If-Match and ETag help avoid overwriting a newer resource version?
Quick answer
An ETag identifies a particular version of a resource. After reading the resource, send its ETag in the If-Match header on the update. The server applies the change only if the current version still matches; if another update changed it, the server can reject the request with 412 Precondition Failed. Then fetch the latest version, reconcile the edits, and try again. [S1]
The question, the details, the next step.
A practical checklist
Retain the ETag from the resource representation you actually edited.
Send that tag in If-Match with the update; do not invent or increment tags.
Treat 412 as a stale-version signal and fetch the latest representation.
Reconcile newer content before preparing a fresh conditional update.
Confirm the specific API’s documented ETag and error behavior.
A few more useful answers
Does an ETag have to be a number that increases with every edit?
No. ETag generation is not prescribed, so do not infer a numeric sequence or create a replacement tag yourself. Use the value the server provides. [S1]
Will If-Match work with a weak ETag?
Does receiving 412 tell me how to merge the changes?
No. It indicates that the condition did not match; the sources do not specify a merge strategy. Fetch the latest representation and reconcile according to your application’s workflow.
Sources
References used for this guide.
- [S1] ETag header - HTTP | MDN developer.mozilla.org · Checked
- [S2] If-Match header - HTTP | MDN developer.mozilla.org · Checked
