400 Bad Request
Fault: Client
TL;DR
The request has malformed syntax or invalid request message framing.
The server cannot process the request due to a client error.
What This Error Means
The server cannot process the request due to a client error.
The request has malformed syntax or invalid request message framing.
Common Causes
- Invalid JSON/XML syntax
- Missing required parameters
- Malformed request headers
- Invalid query string
How to Fix It (For Visitors)
- Check the URL for typos
- Contact the site administrator
How to Fix It (For Developers/Admins)
- Validate request syntax
- Check API request format
- Review Content-Type header
- Validate JSON payload
Code Examples
Here's how to return a 400 status code in various programming languages:
Python (Flask)
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/endpoint')
def endpoint():
return jsonify({"message": "Bad Request"}), 400
Node.js (Express)
const express = require('express');
const app = express();
app.get('/endpoint', (req, res) => {
res.status(400).json({ message: 'Bad Request' });
});
PHP
<?php
http_response_code(400);
header('Content-Type: application/json');
echo json_encode(['message' => 'Bad Request']);
?>
Go
package main
import (
"encoding/json"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(400)
json.NewEncoder(w).Encode(map[string]string{
"message": "Bad Request",
})
}
Java (Spring Boot)
@GetMapping("/endpoint")
public ResponseEntity<Map<String, String>> endpoint() {
Map<String, String> response = new HashMap<>();
response.put("message", "Bad Request");
return ResponseEntity.status(400).body(response);
}
Ruby (Sinatra)
get '/endpoint' do
status 400
json message: 'Bad Request'
end
Browser Compatibility
| Browser | Support | Notes |
|---|---|---|
| Chrome | ✓ Full Support | All versions |
| Firefox | ✓ Full Support | All versions |
| Safari | ✓ Full Support | All versions |
| Edge | ✓ Full Support | All versions |
Official Specification
This status code is defined in RFC 7231 Section 6.5.1.