URL Causing HTTP 431 / 414 Errors

The initial flow worked on small datasets, but once the API returned larger responses, the redirect started failing with errors such as:
HTTP ERROR 431 (Request Header Fields Too Large)
or occasionally:
HTTP ERROR 414 (URI Too Long)
⚠️ Root Cause
The application used a pattern like:
const encoded = encodeURIComponent(JSON.stringify(data));window.location.href = `/result?data=${encoded}`;
This meant the entire API response (often several kilobytes of JSON) was being attached to the URL as a query parameter.
Why this fails
- Most browsers and proxies enforce strict limits on URL length (usually 8–16 KB total, including headers).
- Exceeding that limit triggers a 431 or 414 error, depending on where it fails (proxy vs browser).
- Even before failure, such URLs break analytics, browser history, and caching.
💡 Solution: Store Data Locally, Pass Only a Key
Instead of serializing the full JSON in the URL, the client now saves it in sessionStorage and only includes a short identifier in the query.
Example Implementation
// 1️⃣ Save data locallyconst key = `result_${Date.now()}`;sessionStorage.setItem(key, JSON.stringify(data));// 2️⃣ Redirect using only the keywindow.location.href = `/result?key=${key}`;
Then, on the result page:
// 3️⃣ Read data on loadconst params = new URLSearchParams(window.location.search);const key = params.get("key");if (key) { const stored = sessionStorage.getItem(key); if (stored) { const parsed = JSON.parse(stored); renderResults(parsed);
}
}
✅ Benefits
- URL length reduced from ~10 KB → <100 bytes
- No more
431or414errors - No sensitive data in logs or browser history
- Smooth navigation across repeated searches or actions
- Works consistently in dev, staging, and production environments
🧠 Key Learnings
LessonDescription
Never put large JSON in query strings
Keep URLs lightweight — identifiers only.
Use sessionStorage or localStorage
Great for short-lived client-side data between pages.
Mind proxy limits
Many CDNs, firewalls, and browsers cap header + URL size to ~8 KB.
Clean up after use
Remove old session keys to prevent buildup.
Avoid leaking data
URLs are logged, cached, and shared — never store sensitive info there.
📘 Best Practice Pattern
🧩 Store large payloads locally or on the backend,
🔑 Send only a small key or token in the URL,
💾 Retrieve and render data after navigation,
🚫 Keep URLs under 2 KB total for safety.
✅ Outcome
After refactoring:
- Navigation became instant and reliable.
- The app no longer triggered 431/414 errors.
- The approach now scales regardless of API payload size.
