0
0
Rest APIprogramming~5 mins

400 Bad Request in Rest API

Choose your learning style9 modes available
Introduction

The 400 Bad Request error tells you that the server cannot understand your request because it is wrong or incomplete.

When a user sends a form with missing required fields.
When the data format sent to the server is incorrect.
When the URL query parameters are malformed or invalid.
When the request headers are missing important information.
When the request body contains invalid JSON or XML.
Syntax
Rest API
HTTP/1.1 400 Bad Request
Content-Type: text/plain

Your request is invalid or malformed.

This is a response status line followed by headers and a message body.

The status code 400 means the client made a bad request.

Examples
The server responds with JSON explaining what is wrong.
Rest API
HTTP/1.1 400 Bad Request
Content-Type: application/json

{"error": "Missing required field 'name'"}
The server sends an HTML page explaining the error.
Rest API
HTTP/1.1 400 Bad Request
Content-Type: text/html

<html><body><h1>400 Bad Request</h1><p>Invalid input data.</p></body></html>
Sample Program

This simple server accepts POST requests with JSON data. If the JSON is invalid or missing the 'name' field, it returns a 400 Bad Request with an error message.

Rest API
from http.server import BaseHTTPRequestHandler, HTTPServer
import json

class SimpleHandler(BaseHTTPRequestHandler):
    def do_POST(self):
        content_length = int(self.headers.get('Content-Length', 0))
        body = self.rfile.read(content_length).decode('utf-8')
        try:
            data = json.loads(body)
        except json.JSONDecodeError:
            self.send_response(400)
            self.send_header('Content-Type', 'application/json')
            self.end_headers()
            response = {'error': 'Invalid JSON'}
            self.wfile.write(json.dumps(response).encode('utf-8'))
            return
        if 'name' not in data:
            self.send_response(400)
            self.send_header('Content-Type', 'application/json')
            self.end_headers()
            response = {'error': "Missing required field 'name'"}
            self.wfile.write(json.dumps(response).encode('utf-8'))
            return
        self.send_response(200)
        self.send_header('Content-Type', 'application/json')
        self.end_headers()
        response = {'message': f"Hello, {data['name']}!"}
        self.wfile.write(json.dumps(response).encode('utf-8'))

if __name__ == '__main__':
    server = HTTPServer(('localhost', 8080), SimpleHandler)
    print('Server running on http://localhost:8080')
    server.serve_forever()
OutputSuccess
Important Notes

400 Bad Request means the problem is with the client, not the server.

Always check your request data carefully before sending.

Servers use this code to tell you exactly what is wrong if possible.

Summary

400 Bad Request means the server cannot process your request because it is wrong or incomplete.

Use it when the client sends invalid data or malformed requests.

It helps clients fix their requests by providing error details.