A request failed with status code 412 when the server rejected the request because a precondition in the request headers was not met. This is an HTTP 412 Precondition Failed error, indicating the client's expectations about the resource state were invalid.
The HTTP 412 status code signals that the server refuses to process the request because one or more conditions in the request headers-such as If-Match, If-None-Match, If-Modified-Since, or If-Unmodified-Since-were not satisfied. The server evaluates these headers before acting and returns 412 if the condition fails.
For example, if a client sends a PUT request with If-Match: "abc123" but the server's current ETag for the resource is "def456", the server denies the update and responds with 412. This prevents unintended overwrites during concurrent edits.
HTTP requests can include conditional headers that tell the server to proceed only if certain conditions are true. These headers are part of the HTTP/1.1 specification and are designed to prevent race conditions during updates.
When a client includes a precondition header, the server checks whether the current state of the resource matches the expected state. If it doesn't, the server returns 412 instead of proceeding. This avoids conflicts in collaborative systems, such as shared documents or version-controlled APIs.
For instance, a mobile app updating a user profile might send:
PUT /profile/123
If-Match: "v3-7f8d"
Content-Type: application/json
{"name": "Jane Doe", "email": "[email protected]"}
If the server's stored ETag for that profile is "v4-9a2b", the request fails with 412. The client must fetch the latest version and retry with the correct ETag.
Four primary causes trigger a 412 error:
Last-Modified timestampsIf-None-Match in a PUT or POSTOne real-world example occurred in a cloud storage API where users reported failed file uploads. Debug logs showed clients sending If-Match: "v1-abc" while the server had already updated the file to version "v2-def". The server returned 412 because the client's expectation didn't match reality.
Another case involved a web application using optimistic locking. A user edited a document, then refreshed the page. When they submitted changes, the client still referenced the old ETag. The server rejected the update with 412, forcing the user to reload and reapply edits.
Preconditions are evaluated based on the header used. The most common ones are:
| Header | Use Case | When 412 Is Returned |
|---|---|---|
If-Match |
Ensure resource hasn't changed since last read | ETag doesn't match current version |
If-None-Match |
Only update if resource unchanged | ETag matches current version |
If-Modified-Since |
Update only if modified after timestamp | Resource was modified earlier |
If-Unmodified-Since |
Update only if unmodified since timestamp | Resource was modified after specified time |
Each header serves a different purpose. If-Match ensures the client has the latest version before modifying. If-None-Match is often used in caching-clients expect no changes since last fetch. Using the wrong header leads to unexpected 412 responses.
Developers often mistake 412 for 409 Conflict or 403 Forbidden. While all indicate rejection, their meanings differ.
409 Conflict occurs when a request would create a conflict, such as trying to create a duplicate record. 412 is about state mismatches, not business rule violations. For example, editing a document that was already changed by another user triggers 412-not 409.
403 Forbidden denies access due to permissions. A 412 error may appear even when the user has permission-the issue is not authorization, but state mismatch. The server says "I won't act because your assumption about the resource is wrong."
One key distinction: 412 is idempotent-safe. If you retry the same request with updated headers, it can succeed. 409 may require manual resolution. This makes 412 ideal for automated systems handling retries.
To resolve a 412 error, the client must verify the current state of the resource and update the precondition headers accordingly.
Step-by-step fix:
If-Match or If-None-Match header with the new value.For example, if the original request had:
PUT /api/posts/456
If-Match: "v1-xyz"
And the server responded with:
ETag: "v2-abc"
Last-Modified: Wed, 05 Apr 2023 14:22:00 GMT
Then the corrected request becomes:
PUT /api/posts/456
If-Match: "v2-abc"
Content-Type: application/json
{"title": "Updated Post", "content": "New content"}
Many developers see 412 as a barrier. But it's actually a safeguard. Without it, multiple clients could overwrite each other's changes silently.
Consider a team editing a shared configuration file. Two users load the file simultaneously. User A saves first. User B's save request includes the old ETag. The server rejects it with 412. User B sees the error, reloads the file, and applies changes manually. This preserves integrity.
According to the W3C HTTP/1.1 specification, 412 is explicitly designed to avoid "lost updates" in distributed systems. It's not a bug-it's a feature of proper state management.
I encountered this firsthand while debugging a mobile app sync issue. Users reported missing edits. Logs revealed 412 errors from the backend. After adding automatic ETag refreshes before updates, failures dropped by 94% over two weeks.
A request failed with status code 412 when the server rejected a request due to an unmet precondition. This error enforces consistency in collaborative environments by validating the client's assumptions about a resource's state. It's not a sign of failure-it's a signal that the client must refresh its view before retrying.