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.

Usage

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.

Note

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.

Example

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.

Request

POST /blog/update?postid=111&task=reply HTTP/1.1
Host: www.example.re
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>

Response

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>

Code references

.NET

HttpStatusCode.PreconditionFailed

Rust

http::StatusCode::PRECONDITION_FAILED

Rails

:precondition_failed

Go

http.StatusPreconditionFailed

Symfony

Response::HTTP_PRECONDITION_FAILED

Python3.5+

http.HTTPStatus.PRECONDITION_FAILED

Java

java.net.HttpURLConnection.HTTP_PRECON_FAILED

Apache HttpComponents Core

org.apache.hc.core5.http.HttpStatus.SC_PRECONDITION_FAILED

Angular

@angular/common/http/HttpStatusCode.PreconditionFailed

Takeaway

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.

See also

  • 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.
Last updated: August 2, 2023