HTTP 428 Precondition Required
Overview
The HTTP ~428 Precondition Required~ status code indicates that the server requires the client to apply one or more preconditions to their request headers. It is typically used to enforce certain conditions before processing the request.
Purpose
The HTTP 428 response is used to inform the client that specific preconditions must be met for the server to process the request.
Usage
Client Behavior:
- Send Request: The client sends an HTTP request to the server.
- Receive Response: The client receives the HTTP 428 status code, indicating that preconditions are required.
- Apply Preconditions: The client adjusts the request headers to meet the specified preconditions.
- Resend Request: The client resends the adjusted request to the server.
Server Behavior:
- Evaluate Preconditions: The server determines that the request requires certain preconditions to be met.
- Send Response: The server responds with a ~428 Precondition Required~ status, specifying the required preconditions.
Scenarios
- Conditional Requests: Used when the server wants to enforce specific conditions before processing the request.
Sequence Diagram
Illustrating the process for an HTTP 428 response:
sequenceDiagram participant Client participant Server as Web Server Note over Client: Step 1: Client sends a request Client->>Server: HTTP Request (Step 1) Note over Server: Step 2: Server evaluates preconditions Server->>Client: HTTP/1.1 428 Precondition Required (Step 2) Server->>Client: Preconditions: If-Match, If-None-Match (Step 3)
Curl Request and Response Example
Sending a request that requires preconditions using Curl:
curl -i http://example.com/resource
# Expected response: HTTP/1.1 428 Precondition Required
# Preconditions: If-Match, If-None-Match
PHP cURL Request and Response Example
PHP script using cURL to handle a 428 Precondition Required response:
<?php
$ch = curl_init('http://example.com/resource');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 428) {
$preconditions = curl_getinfo($ch, CURLINFO_PRECONDITION_REQUIRED);
echo "Precondition required. Preconditions: $preconditions";
}
curl_close($ch);
?>
Python Request and Response Example
Python script to handle a 428 Precondition Required response:
import requests
response = requests.get('http://example.com/resource')
if response.status_code == 428:
preconditions = response.headers['Preconditions']
print(f"Precondition required. Preconditions: {preconditions}")
Apache Configuration for HTTP 428 Precondition Required
Configuring Apache to handle scenarios where the server requires preconditions:
<VirtualHost *:80>
ServerName example.com
<Location "/resource">
# Additional configuration to evaluate preconditions
# ...
# Return 428 Precondition Required with Preconditions header
Header always set Preconditions "If-Match, If-None-Match"
ErrorDocument 428 "HTTP/1.1 428 Precondition Required"
</Location>
</VirtualHost>
NGINX Configuration for HTTP 428 Precondition Required
Setting up NGINX to handle scenarios where the server requires preconditions:
server {
listen 80;
server_name example.com;
location /resource {
# Additional configuration to evaluate preconditions
# ...
# Return 428 Precondition Required with Preconditions header
add_header Preconditions "If-Match, If-None-Match";
return 428 "HTTP/1.1 428 Precondition Required";
}
}
HTTP 426 Upgrade Required HTTP 429 Too Many Requests