400 Bad Request
Description
The 400 Bad Request
status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error. This could be due to malformed request syntax, invalid request message framing, or deceptive request routing.
This status code is commonly used when: - The request contains invalid syntax - The request parameters are invalid - The request body is malformed or cannot be parsed - The request is missing required parameters - The client has sent a request that doesn't comply with the application's expectations
The 400 Bad Request
error is a generic client-side error status, indicating that the server understood the content type of the request but was unable to process the contained instructions.
Syntax
The server responds with a 400 Bad Request
status and typically includes an explanation of the error:
HTTP/1.1 400 Bad Request
Content-Type: application/json
Content-Length: [length in bytes]
{
"error": "Bad Request",
"message": "The request could not be understood by the server due to malformed syntax",
"details": [specific details about what was wrong with the request]
}
Examples
Malformed JSON Example
A client sends a request with malformed JSON:
Client Request:
POST /api/users HTTP/1.1
Host: api.example.com
Content-Type: application/json
Content-Length: 42
{
"name": "John Doe",
"email": "[email protected],
}
Server Response:
HTTP/1.1 400 Bad Request
Content-Type: application/json
Content-Length: 160
{
"error": "Bad Request",
"message": "Invalid JSON in request body",
"details": "Unexpected end of JSON input at line 4 column 1"
}
Missing Required Parameter Example
A client sends a request missing a required parameter:
Client Request:
POST /api/orders HTTP/1.1
Host: shop.example.com
Content-Type: application/json
Content-Length: 45
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
{
"items": [
{"product_id": 123, "quantity": 2}
]
}
Server Response:
HTTP/1.1 400 Bad Request
Content-Type: application/json
Content-Length: 138
{
"error": "Bad Request",
"message": "Missing required parameter",
"details": "The 'shipping_address' field is required for creating an order"
}
Invalid Parameter Value Example
A client sends a request with an invalid parameter value:
Client Request:
Server Response:
HTTP/1.1 400 Bad Request
Content-Type: application/json
Content-Length: 172
{
"error": "Bad Request",
"message": "Invalid parameter values",
"details": {
"page": "Must be a positive integer",
"limit": "Must be between 1 and 100"
}
}
Summary
The 400 Bad Request
status code is a fundamental part of HTTP error handling, providing a standard way to indicate that a client's request cannot be processed due to issues with the request itself. It helps clients understand that they need to modify their request before resubmitting it, rather than indicating a server-side problem. By including specific details about what went wrong, servers can guide clients toward correcting their requests, improving the overall robustness of web applications and APIs.