412 Precondition Failed
A condition in the request headers, such as If-Match or If-Unmodified-Since, was false, so the server did not apply the request.
What 412 Precondition Failed means
Clients use conditional headers to avoid overwriting other people's changes. A PUT with If-Match: "abc" means update only if the current ETag is still "abc". If someone else changed it, the server answers 412.
This prevents the lost update problem. For GET and HEAD, a failed If-None-Match or If-Modified-Since gives 304 instead.
Common causes
- The resource changed since you fetched it, so your ETag no longer matches.
- A client sending a stale or malformed If-Match or If-Unmodified-Since value.
How to fix 412 Precondition Failed
- Fetch the resource again to get the current ETag, reapply your change and retry.
- In apps, tell the user someone else edited the item and let them merge.
- Make sure servers behind a load balancer generate the same ETag for the same content.
Headers that go with it
ETagIf-MatchIf-Unmodified-Since
Example response
HTTP/1.1 412 Precondition Failed
ETag: "33a64df5"
Content-Type: text/plain; charset=utf-8
412 Precondition FailedHow to check for a 412 response
cURL
# Print only the status code
curl -s -o /dev/null -w "%{http_code}\n" https://example.com/JavaScript (Node.js)
const res = await fetch("https://example.com/");
if (res.status === 412) {
console.log(res.status, res.statusText);
}Python
import requests
res = requests.get("https://example.com/")
if res.status_code == 412:
print(res.status_code, res.reason)Go
res, err := http.Get("https://example.com/")
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode == 412 {
fmt.Println(res.Status)
}Specification
412 Precondition Failed is defined in RFC 9110, section 15.5.13.
Related status codes
- 409
The request conflicts with the current state of the resource, such as saving a version that someone else already changed.
- 428
The server requires a conditional request, such as one with If-Match, to stop clients from overwriting each other's changes.
- 304
The cached copy is still valid, so the server sends no body and the client loads the resource from its own cache.