The 412 Precondition Failed HTTP status code indicates that one or more conditions specified in the request headers were not met by the server. This response is commonly associated with conditional requests using headers like If-Match, If-Unmodified-Since, or If-None-Match.
A client sends a request with a precondition header. If the condition evaluates to false, the server rejects the request with status code 412.
// Example of a conditional GET request using Fetch API
fetch("/api/resource", {
headers: {
"If-Match": "etag-value"
}
})
.then(res => {
if (res.status === 412) {
console.log("Precondition failed");
}
});
If the ETag sent in If-Match does not match the server’s current version, the server responds with 412 Precondition Failed and no data is modified.
Scenario: The server expects the specific ETag "v1" to allow updates.
// Simulated server-side condition check
const clientETag = "v1"; // The Simulated Server Version
function checkCondition(inputETag) {
if (inputETag !== clientETag) {
return "412 Precondition Failed";
}
return "200 OK - Update Success";
}
If-Match values