AZURE

BadRequest - Bad Request: Malformed Request

Your request format violates ARM's API contract—malformed JSON syntax, missing required headers (Content-Type: application/json or Authorization: Bearer), wrong parameter types, or invalid API version format (must match YYYY-MM-DD or YYYY-MM-DD-preview). This 400 client-side error occurs when ARM can't parse or validate the request structure. ARM checks JSON syntax first, then required headers, parameter types, and request body size limits. Unlike InvalidRequestContent (which means valid JSON but wrong schema), BadRequest indicates the request structure itself is broken. Appears in VM creation, AKS cluster configuration, Azure SQL database setup, and App Service app settings.

#Common Causes

  • Malformed JSON Syntax: The request body contains invalid JSON (unclosed brackets, trailing commas, invalid escape sequences, etc.). ARM can't parse the JSON, so it rejects the request before processing.
  • Missing Required Headers: The request is missing Content-Type: application/json or Authorization: Bearer <token> headers. ARM requires these headers for most operations.
  • Parameter Type Mismatch: Properties have the wrong data type. A field expecting an integer but receiving a string, or an array where an object is expected, causes this. ARM validates types before processing the request.
  • Invalid API Version Format: The API version doesn't match the required format (YYYY-MM-DD or YYYY-MM-DD-preview). API versions must follow this exact pattern.

Solutions

  1. 1Step 1: Diagnose - Validate JSON syntax using JSON.parse() to verify your request body is valid JSON. Check for unclosed brackets, trailing commas, invalid escape sequences.
  2. 2Step 2: Diagnose - Check required headers are present: Content-Type: application/json and Authorization: Bearer <token>. Inspect your request headers.
  3. 3Step 3: Diagnose - Verify API version format matches YYYY-MM-DD or YYYY-MM-DD-preview pattern (e.g., "2023-01-01" or "2023-01-01-preview").
  4. 4Step 4: Fix - Fix JSON syntax errors. Use a JSON validator to identify and correct syntax issues.
  5. 5Step 5: Fix - Add missing headers. Ensure both Content-Type and Authorization headers are present with correct values.
  6. 6Step 6: Fix - Convert parameter values to the correct type. If a field expects an integer, ensure it's a number, not a string.
  7. 7Step 7: Verify - Retry your request. It should succeed with HTTP 200/201 instead of 400 BadRequest.

</>Code Examples

Request Validation and Format Check
1# This script helps diagnose BadRequest errors by validating request format
2
3# Step 1: Validate JSON syntax (example JSON file)
4JSON_FILE="request-body.json"
5echo "Validating JSON file: $JSON_FILE"
6
7if [ -f "$JSON_FILE" ]; then
8  # Check if file is valid JSON using Python (if available)
9  if command -v python3 &> /dev/null; then
10    if python3 -m json.tool $JSON_FILE > /dev/null 2>&1; then
11      echo "JSON syntax is valid"
12    else
13      echo "ERROR: Invalid JSON syntax"
14      python3 -m json.tool $JSON_FILE 2>&1 | head -10
15      exit 1
16    fi
17  else
18    echo "Python not available. Use an online JSON validator or jq:"
19    echo "  jq . $JSON_FILE"
20  fi
21else
22  echo "JSON file not found: $JSON_FILE"
23fi
24
25# Step 2: Check API version format
26API_VERSION="2023-01-01"
27echo "Validating API version format: $API_VERSION"
28
29if [[ $API_VERSION =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}(-preview)?$ ]]; then
30  echo "API version format is valid"
31else
32  echo "ERROR: Invalid API version format"
33  echo "Required format: YYYY-MM-DD or YYYY-MM-DD-preview"
34  echo "Example: 2023-01-01 or 2023-01-01-preview"
35  exit 1
36fi
37
38# Step 3: Validate request body size (example: 1MB limit)
39MAX_SIZE=1048576  # 1MB in bytes
40if [ -f "$JSON_FILE" ]; then
41  FILE_SIZE=$(stat -f%z "$JSON_FILE" 2>/dev/null || stat -c%s "$JSON_FILE" 2>/dev/null)
42  if [ ! -z "$FILE_SIZE" ]; then
43    if [ $FILE_SIZE -gt $MAX_SIZE ]; then
44      echo "WARNING: Request body size ($FILE_SIZE bytes) exceeds limit ($MAX_SIZE bytes)"
45    else
46      echo "Request body size is within limit: $FILE_SIZE bytes"
47    fi
48  fi
49fi
50
51# Step 4: Check required headers (for REST API calls)
52echo ""
53echo "Required headers for ARM API calls:"
54echo "  1. Content-Type: application/json"
55echo "  2. Authorization: Bearer <token>"
56echo ""
57echo "To get an access token:"
58echo "  az account get-access-token --query accessToken -o tsv"
59
60# Step 5: Validate ARM template (if using templates)
61TEMPLATE_FILE="template.json"
62PARAMS_FILE="params.json"
63if [ -f "$TEMPLATE_FILE" ]; then
64  echo "Validating ARM template..."
65  if command -v az &> /dev/null; then
66    # Note: This requires a resource group - adjust as needed
67    # az deployment group validate \
68    #   --resource-group my-rg \
69    #   --template-file $TEMPLATE_FILE \
70    #   --parameters @$PARAMS_FILE
71    echo "Use 'az deployment group validate' to validate your template"
72  fi
73fi
74
75# Step 6: Common JSON syntax errors to check
76echo ""
77echo "Common JSON syntax errors:"
78echo "  1. Trailing commas: { 'key': 'value', }  (remove trailing comma)"
79echo "  2. Unclosed brackets: { 'key': 'value'    (missing closing brace)"
80echo "  3. Invalid escape sequences: 'path\file'  (use 'path\\file')"
81echo "  4. Single quotes instead of double quotes: { 'key': 'value' }  (use double quotes)"
82echo "  5. Comments in JSON: { /* comment */ }  (JSON doesn't support comments)"

Related Errors

Provider Information

This error code is specific to AZURE services. For more information, refer to the official AZURE documentation.

BadRequest - Bad Request: Malformed Request | AZURE Error Reference | Error Code Reference