The HTTP 412 status code stands for "Precondition Failed". It is a client error response indicating that one or more conditions specified in the request's If-Precondition or If-Match header fields were not satisfied. In other words, the server could not fulfill the request because a precondition specified by the client was not met.

When is HTTP 412 Used?

  • When a client sends a request with a conditional header (e.g., If-Match, If-None-Match, If-Modified-Since) and the condition is not satisfied.
  • Typically used in optimistic concurrency control or in scenarios where a client wants to make sure it is working with the most up-to-date version of a resource.
  • For example, a client may only want to proceed with an action if a resource has not changed since a specific date or if the resource's version matches a specific identifier.

Common Use Cases

  • Concurrent updates: A client tries to update a resource only if the resource has not been modified by another client since the client last retrieved it.
  • Conditional GET requests: A client retrieves a resource only if it has been modified since a certain date or version.
  • Etag-based concurrency control: The client specifies an If-Match or If-None-Match header, and if the ETag (resource version) does not match the condition, a 412 error is returned.

Example Scenarios

  • A client attempts to update a resource but only if the resource version has not changed. If the version has changed, the server returns a 412 error to prevent the update.
  • A client sends a GET request with an If-Modified-Since header, but the resource has not been modified since the given date, causing the server to respond with a 412 error.

Example

Client Request (Conditional Request with If-Match)

    
        PUT /products/123 HTTP/1.1
        Host: example.com
        If-Match: "abc123"
        Content-Type: application/json
        {
          "name": "Updated Product Name"
        }
    

(The client tries to update a product only if the product's version is "abc123.")

Server Response (412 Precondition Failed)

    
        HTTP/1.1 412 Precondition Failed
        Content-Type: application/json

        {
          "error": "The resource version has changed. The update cannot be applied."
        }
    

Best Practices for Handling HTTP 412

  • Ensure proper versioning: If using ETags or version identifiers, ensure they are correctly managed and reflect the actual state of resources.
  • Communicate the failure: Provide a clear error message that explains which condition was not met (e.g., "The resource version has changed").
  • Retry logic: In some cases, the client may want to re-fetch the resource and reapply its changes if a condition fails.
  • Conditional requests: Make sure that conditional headers are used correctly to prevent unnecessary server load and avoid conflicts.

Summary

HTTP 412 indicates that the request could not be fulfilled because the preconditions specified by the client (usually in conditional headers like If-Match, If-Modified-Since, or If-None-Match) were not met. It helps prevent conflicts in scenarios like concurrent updates or optimistic concurrency control, ensuring that operations are only performed when the conditions are satisfied.