What is HTTP Response Status Code 400 Bad Request?
HTTP response status code 400 Bad Request indicates that the server cannot process the request due to malformed syntax or an invalid request message. The client should correct the request and try again.
When is HTTP Response Status Code 400 Bad Request Used?
The 400 status code is used when the server detects a client error, such as missing required parameters, invalid JSON format, or incorrect query syntax.
Example 1: Missing Required Parameter
# Client sends a request with a missing parameter. GET /api/user?id= HTTP/1.1 Host: api.example.com # Server Response HTTP/1.1 400 Bad Request Date: Wed, 09 Oct 2024 14:00:00 GMT Server: Apache/2.4.41 (Ubuntu) Content-Type: application/json { "error": "Missing 'id' parameter" }
Example 2: Invalid JSON Format
# Client sends a POST request with invalid JSON. POST /api/create-user HTTP/1.1 Host: api.example.com Content-Type: application/json { "name": "John Doe", "email": "[email protected]", // Extra comma here causes error. # Server Response HTTP/1.1 400 Bad Request Date: Wed, 09 Oct 2024 14:05:00 GMT Server: Nginx/1.18.0 Content-Type: application/json { "error": "Invalid JSON format" }
Summary
The HTTP 400 Bad Request status code is used when the server cannot understand the request due to client-side errors. It helps inform the client of mistakes so they can be corrected and resent.