HTTP status code 412 “Precondition Failed” indicates that the server has refused to perform the request because one or more access conditions specified in the request’s headers evaluated to false. This status code is often used in conditional requests, where the client includes one or more header fields to specify conditions under which the request should be processed.
Common Scenarios for 412 Status Code
-
Conditional Requests: When a client makes a conditional request using headers like
If-Match
,If-Unmodified-Since
, orIf-None-Match
, and the conditions are not met, the server responds with 412 Precondition Failed. -
Web Versioning: In systems that use entity tags (ETags) for versioning, if the client’s specified ETag does not match the current version of the resource, the server will return a 412 status.
-
Optimistic Concurrency Control: This status is used in optimistic concurrency control, where a client performs an action based on a resource’s state and expects that state to remain unchanged until the client’s request is processed.
-
Prevent Stale Reads: When a client requests data with a condition that the data must be fresh (e.g., not cached), and the server cannot guarantee this freshness, it may return a 412 status.
Characteristics of 412 Precondition Failed
-
Client Error: A 412 status code is a client error, indicating that the request could not be completed due to incorrect headers.
-
Conditional Requests: It often results from a conditional GET request, where the conditions specified by the client are not satisfied.
-
Preconditions: The request typically includes one or more precondition headers that must be true for the server to process the request.
-
Idempotent Requests: Methods like GET, HEAD, and DELETE are idempotent and should not cause a 412 error unless there is a server misconfiguration.
Example Response
HTTP/1.1 412 Precondition Failed
Content-Type: text/html
Content-Length: 178
<html>
<head><title>412 Precondition Failed</title></head>
<body>
<h1>412 Precondition Failed</h1>
<p>The precondition on the request for the URL evaluated to false. It is either not set or not set to the correct value.</p>
</body>
</html>
In this example, the server indicates that the request cannot be processed because the precondition specified by the client is not met. The client should review the conditions it has specified and resend the request with the correct headers if necessary.
Note: Clients receiving a 412 Precondition Failed status should check their request headers for any conditional requests that may be specified incorrectly. If the client intended to perform the request without conditions, it should remove any precondition headers and resend the request. Additionally, clients should be aware that some servers may not support all conditional request headers, which could also lead to a 412 error.
Related 4xx error code
- 400 Bad Request
- 401 Unauthorized
- 402 Payment Required
- 403 Forbidden
- 404 Not Found
- 405 Method Not Allowed
- 406 Not Acceptable
- 407 Proxy Authentication Required
- 408 Request Timeout
- 409 Conflict
- 410 Gone
- 411 Length Required
- 413 Payload Too Large
- 414 URI Too Long
- 415 Unsupported Media Type
- 416 Range Not Satisfiable
- 417 Expectation Failed
- 418 I’m a teapot
- 421 Misdirected Request
- 422 Unprocessable Entity (WebDAV)
- 423 Locked (WebDAV)
- 424 Failed Dependency (WebDAV)
- 425 Too Early
- 426 Upgrade Required
- 428 Precondition Required
- 429 Too Many Requests
- 431 Request Header Fields Too Large
- 451 Unavailable For Legal Reasons
- 499 Client Closed Request