HTTP 304 Not Modified
Overview
The HTTP 304 Not Modified
status code is used in conditional GET requests to indicate that the resource has not been modified since the last request. This status is used for caching purposes to reduce bandwidth and processing.
Purpose
The HTTP 304 response is primarily used to inform the client that the cached version of the requested resource is still valid and can be used, eliminating the need to transfer the same data again.
Usage
Client Behavior:
- Send Conditional Request: The client sends a conditional GET request typically including
If-Modified-Since
orIf-None-Match
headers. - Receive Response: The client receives the HTTP 304 status code if the resource has not been modified.
Server Behavior:
- Evaluate Request Headers: The server checks the request headers against the current state of the resource.
- Send Response: If the resource has not changed, the server sends a
304 Not Modified
response without a message body.
Scenarios
- Resource Caching: To indicate that a cached resource is still valid and does not require re-downloading.
- Bandwidth Optimization: Reducing bandwidth usage by not sending unchanged resources.
Sequence Diagram
Illustrating the process for an HTTP 304 response:
sequenceDiagram participant Client participant Server as Web Server Note over Client: Client sends a conditional GET request Client->>Server: GET /resource HTTP/1.1 Client->>Server: If-Modified-Since: [Last-Modified Date] Note over Server: Server checks resource status Server->>Client: HTTP/1.1 304 Not Modified
Curl Request and Response Example
Sending a conditional GET request using Curl to check if a resource has been modified:
curl -i -H "If-Modified-Since: [Last-Modified Date]" http://example.com/resource
# Expected response: HTTP/1.1 304 Not Modified
PHP cURL Request and Response Example
PHP script using cURL to make a conditional GET request:
<?php
$ch = curl_init('http://example.com/resource');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('If-Modified-Since: [Last-Modified Date]'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 304) {
echo "Resource has not been modified.";
}
curl_close($ch);
?>
Python Request and Response Example
Python script to send a conditional GET request and check for a 304 Not Modified response:
import requests
headers = {'If-Modified-Since': '[Last-Modified Date]'}
response = requests.get('http://example.com/resource', headers=headers)
if response.status_code == 304:
print("Resource has not been modified")
Apache Configuration for HTTP 304 Not Modified
Apache server configuration to support conditional GET requests and 304 responses:
<VirtualHost *:80>
ServerName example.com
# Ensure proper handling of If-Modified-Since header
# ...
</VirtualHost>
NGINX Configuration for HTTP 304 Not Modified
Setting up NGINX to handle 304 Not Modified responses for conditional GET requests:
server {
listen 80;
server_name example.com;
location /resource {
# Configurations for handling If-Modified-Since header
# ...
}
}
HTTP 303 See Other HTTP 305 Use Proxy