Understanding and resolving malformed request errors.
The HTTP 400 "Bad Request" error is one of the most common client errors on the web. It indicates the server could not understand or process the request due to incorrect syntax, malformed data, or invalid parameters.
Unlike 5xx errors that signal a server problem, the 400 code points to a client-side issue. This could be a poorly encoded URL, an invalid JSON body, incorrect HTTP headers, or corrupted cookies. Identifying the exact cause requires methodical analysis.
For REST APIs and modern web applications, monitoring 400 errors is essential. A sudden spike in 400s can indicate a bug in your frontend, an incompatibility after an update, or an attack attempting to crash your system.
400 errors can come from many sources. Here are the most common causes:
To identify the exact cause of a 400 error, follow this methodology:
Based on the identified cause, here are the solutions to apply:
Here are code examples to diagnose and prevent 400 errors:
// JavaScript - Validate JSON before sending
function safeSendJSON(url, data) {
try {
const jsonString = JSON.stringify(data);
JSON.parse(jsonString); // Validity check
return fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: jsonString
});
} catch (e) {
console.error("Invalid JSON:", e.message);
throw new Error("Invalid data");
}
}
// PHP - Server-side validation
$json = file_get_contents("php://input");
$data = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
http_response_code(400);
echo json_encode(["error" => "Invalid JSON"]);
exit;
}
Both client AND server-side validation is essential. Client-side for better UX, server-side for security.
Prevention is better than cure. Here are best practices:
It's a client error (4xx codes). The server is working correctly but cannot process the request because it's malformed. It's up to the client to fix their request.
Configure a MoniTao monitor with a valid request expecting HTTP 200. If your endpoint returns 400, you'll be alerted immediately. For APIs, create monitors with the correct body format and authentication.
Likely causes: corrupted cookies (try private browsing), frontend/backend update with format incompatibility, or stricter validation added server-side.
Return a structured JSON: {"error": "description", "field": "field_name", "details": "explanatory message"}. This helps enormously with client-side debugging.
Not directly for public pages that should return 200. However, if your public URLs return 400 due to poorly handled parameters, those pages won't be indexed.
400 indicates a syntactically incorrect request (malformed JSON). 422 indicates a well-formed but semantically invalid request (email in wrong format inside valid JSON).
The HTTP 400 Bad Request error signals something is wrong with the request sent to the server. Best practice is to validate data client-side before sending, and provide explicit error messages server-side for easier diagnosis.
With MoniTao, monitor your critical endpoints and get alerted as soon as a 400 error appears. Proactive monitoring helps detect integration issues, post-update incompatibilities, and exploitation attempts before they impact your users.
Start free, no credit card required.