412

Precondition Failed

Overview

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.

When it happens

Request / Response example

Request
PUT /docs/42 HTTP/1.1
Host: api.example.com
If-Match: "a1b2c3"
Content-Type: application/json

{"title":"Updated"}
Response
HTTP/1.1 412 Precondition Failed
Content-Type: application/json

{"error":"etag_mismatch","current":"\"z9y8x7\""}

In code

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"}'

Common causes

How to fix

Notes

Related status codes

Related headers

Specification