Precondition Failed
The client has indicated preconditions in its headers which the server does not meet.
What it means
The HTTP 412 Precondition Failed client error response code indicates that access to the target resource has been denied. This happens with conditional requests on methods other than GET or HEAD when the condition defined by the `If-Unmodified-Since` or `If-Match` headers is not fulfilled.
How it works
- 1A client downloads a Wiki page (Version 1).
- 2The client makes edits, then sends a PUT request with 'If-Match: Version-1'.
- 3Meanwhile, someone else updated the page to Version 2.
- 4The server sees the preconditions don't match, and returns 412.
Why you're seeing this
- • The resource was modified on the server since the client last fetched it.
How to fix it
- • The client should fetch the newest version of the resource, merge their local changes, and retry the request with the new ETag.
Framework Implementations
Express.js
app.put('/doc', async (req, res) => {
const doc = await getDoc();
if (req.headers['if-match'] !== doc.etag) {
return res.status(412).send('Precondition Failed');
}
});Real-Life Scenarios
Use this to implement Optimistic Concurrency Control (preventing the 'lost update' problem) on API endpoints where multiple users might edit the same resource.
Preventing Overwrites
Two admins are editing the same product description. Admin A saves first. When Admin B hits save, the server sees their 'If-Match' ETag is outdated and returns 412, preventing them from overwriting Admin A's work.
HTTP Transaction
1. Client Request
PUT /wiki/HTTP_412 HTTP/1.1 If-Match: "v123" (new content)
2. Server Response
HTTP/1.1 412 Precondition Failed
Content-Type: application/json
{"error": "The document was modified by another user."}SEO Implications
None.