412 Precondition Failed means a condition in the request headers (If-Match, If-Unmodified-Since, etc.) evaluated to false on the server, so the request was not performed. It most commonly arises in optimistic concurrency control when someone else modified the resource first.
If the client sends a previously fetched ETag via If-Match but the resource has since changed and its current ETag differs, the server returns 412 to prevent overwriting. This guards against the lost-update problem.
PUT /docs/42 HTTP/1.1
Host: api.example.com
If-Match: "a1b2c3"
Content-Type: application/json
{"title":"Updated"}HTTP/1.1 412 Precondition Failed
Content-Type: application/json
{"error":"etag_mismatch","current":"\"z9y8x7\""}app.put('/docs/:id', (req, res) => {
const doc = db.get(req.params.id);
if (req.headers['if-match'] !== doc.etag) {
return res.status(412).json({ error: 'etag_mismatch', current: doc.etag });
}
db.update(req.params.id, req.body);
res.json({ ok: true });
});curl -X PUT https://api.example.com/docs/42 \
-H 'If-Match: "a1b2c3"' \
-H 'Content-Type: application/json' \
-d '{"title":"Updated"}'