412 Precondition Failed
HTTP response status code 412 Precondition Failed is a client error that is returned by the server to indicate that one or more of the client-specified conditions in the request has failed. Due to this failure, the request was not completed.
When the 412 Precondition Failed error message is received, the client will know that one or more of the conditions that are specified in the request has failed, and as such, the resource was not in the expected state. This happens for HTTP requests that are not of type HTTP method HEAD or HTTP method GET.
This is similar to response 304 Not Modified, although in that case, a resource was not sent to the client in response to an HTTP method HEAD or GET request because the client already had the most recent version available, and a subsequent transmission will be a waste of bandwidth.
Search engines like Google will not index a URL with 412 Precondition Failed response status, and consequently, URLs that have been indexed in the past but are now returning this HTTP status code will be removed from the search results.
In the example, the client wants to reply to a blog post, but only under the condition that the post has not been changed since the client last viewed it. The condition is contained as part of the If-Unmodified-Since header. When it fails, it is because the resource has indeed been modified, and the server responds with 412 Precondition Failed to indicate that the request was not completed.
By following this protocol, the problem of having a ‘lost update’ is avoided. Lost updates occur when multiple people are writing to the same resource, and one or more is working with an outdated version. By ensuring that the resource has not been modified, they are guaranteed not to overwrite or modify data that has been modified in the interim.
It is easy to imagine that without this in place, notes in a live document can be overwritten because a subsequent update had been initiated before the start and finish of a shorter one. The shorter one will be overwritten because the subsequent post has cached an older version and with its updates applied, rewrite the entire file.
POST /blog/update?postid=111&task=reply HTTP/1.1 Host: www.example.ai If-Unmodified-Since: Fri, 1 Jan 2021 00:00:00 GMT Content-Type: text/plain Content-Length: 45 <Message body contains reply-text from the client>
HTTP/1.1 412 Precondition Failed Content-Type: text/html Content-Length: 182 <html> <head> <title>Blog Update Error</title> </head> <body> <p>The post has changed since you last viewed it, so your reply will not be recorded.</p> </body> </html>
Apache HttpComponents Core
The 412 Precondition Failed status code is a client error sent in response to an HTTP request that is not of type HTTP method HEAD or HTTP method GET, indicating that one of the specified conditions has failed and that the request will not be completed.
- RFC 7232 HTTP response status code 412 Precondition Failed is a client error that is returned by the server to indicate that one or more of the client-specified conditions in the request has failed. Due to this failure, the request was not completed.