error http
HTTP 408 Request Timeout
Understanding HTTP 408 Request Timeout - the server timed out waiting for the client to send the complete request.
What It Means
HTTP 408 Request Timeout indicates that the server did not receive a complete request message within the time it was willing to wait. The server is closing the connection because the client took too long to send the full request.
This is different from 504 Gateway Timeout, which means an upstream server timed out. A 408 means the client itself was too slow.
Common Causes
- Slow or unstable network connection
- Client paused while uploading a large file
- Client opened a connection but never sent the request
- Network interruption during request transmission
- Server timeout configured too aggressively
- Large request bodies on slow connections
How to Fix
Increase server timeout settings
# Nginx - increase client timeouts
http {
client_body_timeout 60s; # Time to receive request body
client_header_timeout 60s; # Time to receive request headers
send_timeout 60s; # Time between two write operations
}
// Node.js/Express - increase timeout
const server = app.listen(3000);
server.timeout = 120000; // 120 seconds
server.requestTimeout = 120000;
server.headersTimeout = 120000;
Client-side: handle timeouts gracefully
// Using AbortController for client-side timeout
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 30000);
try {
const response = await fetch('/api/data', {
signal: controller.signal
});
clearTimeout(timeoutId);
return await response.json();
} catch (error) {
if (error.name === 'AbortError') {
console.log('Request timed out');
// Retry logic
}
}
Retry with exponential backoff
async function fetchWithRetry(url, options = {}, retries = 3) {
for (let i = 0; i < retries; i++) {
try {
const response = await fetch(url, options);
if (response.status === 408) {
const delay = Math.pow(2, i) * 1000;
await new Promise(resolve => setTimeout(resolve, delay));
continue;
}
return response;
} catch (error) {
if (i === retries - 1) throw error;
}
}
}
Python requests timeout
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
# Set explicit timeouts
response = requests.get(
"https://api.example.com/data",
timeout=(5, 30) # (connect timeout, read timeout)
)
# Retry strategy
session = requests.Session()
retries = Retry(total=3, backoff_factor=1, status_forcelist=[408, 500, 502, 503])
session.mount("https://", HTTPAdapter(max_retries=retries))