HTTP 400 Bad Request

Overview

The HTTP 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 (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

Purpose

The HTTP 400 response is used to signify that the server identified an error in the client’s request. It is a generic error response for situations where the specific error is not suitable for disclosure to the client.

Usage

Client Behavior:

  1. Send Request: The client sends an HTTP request to the server.
  2. Receive Response: The client receives the HTTP 400 status code, indicating an issue with the request.

Server Behavior:

  1. Evaluate Request: The server evaluates the client’s request.
  2. Send Response: If the server finds an issue with the request, it sends a 400 Bad Request response, typically with details about the error.

Scenarios

  • Malformed Syntax: When the request has syntax errors or is malformed.
  • Invalid Parameters: Requests with invalid parameters or query strings.
  • Deceptive Routing: If the request attempts deceptive routing or contains misleading fields.

Sequence Diagram

Illustrating the process for an HTTP 400 response:

sequenceDiagram
    participant Client
    participant Server as Web Server

    Note over Client: Client sends a malformed request
    Client->>Server: GET /resource?invalid=param HTTP/1.1
    Note over Server: Server evaluates and finds an error
    Server->>Client: HTTP/1.1 400 Bad Request

Curl Request and Response Example

Sending a malformed request using Curl to simulate a 400 Bad Request:

curl -i http://example.com/resource?invalid=param
# Expected response: HTTP/1.1 400 Bad Request

PHP cURL Request and Response Example

PHP script using cURL to handle a response for a bad request:

<?php
$ch = curl_init('http://example.com/resource?invalid=param');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 400) {
    echo "Bad request error.";
}
curl_close($ch);
?>

Python Request and Response Example

Python script to send a GET request that results in a 400 Bad Request response:

import requests
response = requests.get('http://example.com/resource', params={'invalid': 'param'})
if response.status_code == 400:
    print("Bad request error")

Apache Configuration for HTTP 400 Bad Request

Apache server configuration to handle bad requests:

<VirtualHost *:80>
    ServerName example.com
    # Additional configurations for handling bad requests
    # ...
</VirtualHost>

NGINX Configuration for HTTP 400 Bad Request

Setting up NGINX to properly respond to bad requests:

server {
    listen 80;
    server_name example.com;
    location / {
        # Configurations to handle malformed requests
        # ...
    }
}

HTTP 308 Permanent Redirect (Experimental) HTTP 401 Unauthorized


 

Free Weekly

Newsletter

Join my weekly newsletter for the latest in tech! You'll get neat coding tricks, trend updates, career advice, SaaS reviews, crypto, bitcoin, and financial tips. All straight to your inbox, designed to keep you ahead.