HTTP 412 Precondition Failed

Overview

The HTTP 412 Precondition Failed status code indicates that one or more conditions given in the request header fields evaluated to false when tested on the server. This response is primarily used in scenarios involving conditional requests and version control.

Purpose

The HTTP 412 response is used to inform the client that one or more conditions specified in the request’s conditional headers, like If-Match, If-None-Match, If-Modified-Since, If-Unmodified-Since, were not met.

Usage

Client Behavior:

  1. Send Conditional Request: The client sends an HTTP request with conditional headers.
  2. Receive Response: The client receives the HTTP 412 status code if the server’s evaluation of the conditions fails.

Server Behavior:

  1. Evaluate Preconditions: The server checks the preconditions specified in the request headers.
  2. Send Response: If the preconditions fail, the server responds with a 412 Precondition Failed status code.

Scenarios

  • Conditional GET: Used in cache validation by sending If-None-Match with an ETag.
  • Optimistic Concurrency Control: When updating resources, If-Match with an ETag ensures that modifications are made to the same version of the resource known to the client.

Sequence Diagram

Illustrating the process for an HTTP 412 response:

sequenceDiagram
    participant Client
    participant Server as Web Server

    Note over Client: Client sends a conditional request
    Client->>Server: GET /resource HTTP/1.1
    Client->>Server: If-None-Match: "etag-value"
    Note over Server: Server evaluates condition
    Server->>Client: HTTP/1.1 412 Precondition Failed

Curl Request and Response Example

Sending a conditional request using Curl:

curl -i -H "If-None-Match: \"etag-value\"" http://example.com/resource
# Expected response: HTTP/1.1 412 Precondition Failed

PHP cURL Request and Response Example

PHP script using cURL to handle a 412 Precondition Failed response:

<?php
$ch = curl_init('http://example.com/resource');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('If-None-Match: "etag-value"'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 412) {
    echo "Precondition failed.";
}
curl_close($ch);
?>

Python Request and Response Example

Python script to send a conditional GET request and handle a 412 response:

import requests
headers = {'If-None-Match': 'etag-value'}
response = requests.get('http://example.com/resource', headers=headers)
if response.status_code == 412:
    print("Precondition failed")

Apache Configuration for HTTP 412 Precondition Failed

Configuring Apache to support conditional requests:

<VirtualHost *:80>
    ServerName example.com
    # Apache configurations for supporting ETags and conditional headers
    # ...
</VirtualHost>

NGINX Configuration for HTTP 412 Precondition Failed

Setting up NGINX to manage conditional requests and 412 responses:

server {
    listen 80;
    server_name example.com;
    location /resource {
        # NGINX configurations for ETags and conditional headers
        # ...
    }
}

HTTP 411 Length Required HTTP 413 Request Entity Too Large


 

Free Weekly

Newsletter

Join my weekly newsletter for the latest in tech! You'll get neat coding tricks, trend updates, career advice, SaaS reviews, crypto, bitcoin, and financial tips. All straight to your inbox, designed to keep you ahead.