High severity HTTP 422 beginner · Fix: 2-5 min

fastapi.exceptions.RequestValidationError

fastapi.exceptions.RequestValidationError

What this error means
FastAPI failed to decode the incoming request body as valid JSON, causing a validation error.

Stack trace

traceback
fastapi.exceptions.RequestValidationError: 1 validation error for Request
body -> json
  Expecting value: line 1 column 1 (char 0) (type=value_error.jsondecode)
QUICK FIX
Validate and fix the client request body to send well-formed JSON with the correct Content-Type header.

Why it happens

FastAPI expects the request body to be valid JSON when the endpoint declares a JSON payload. If the client sends malformed JSON, empty body, or incorrect content-type headers, the JSON decoder raises this error.

Detection

Monitor for 422 HTTP responses with JSON decode errors in logs or middleware to catch malformed JSON requests before processing.

Causes & fixes

1

Client sends malformed or incomplete JSON in the request body

✓ Fix

Ensure the client sends properly formatted JSON with correct syntax and no trailing commas or missing braces.

2

Request Content-Type header is missing or not set to 'application/json'

✓ Fix

Set the Content-Type header to 'application/json' in the client request to inform FastAPI to parse the body as JSON.

3

Empty request body sent when JSON is expected

✓ Fix

Send a valid JSON object or array in the request body; do not send empty body if the endpoint requires JSON.

4

Incorrect Pydantic model or missing required fields causing validation to fail after JSON decode

✓ Fix

Verify the Pydantic model matches the expected JSON structure and all required fields are provided by the client.

Code: broken vs fixed

Broken - triggers the error
python
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float

@app.post("/items/")
async def create_item(item: Item):
    return item

# Client sends invalid JSON or missing Content-Type, triggers JSON decode error
Fixed - works correctly
python
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float

@app.post("/items/")
async def create_item(item: Item):
    return item

# Ensure client sends valid JSON with Content-Type: application/json header
# No code change needed server-side; fix client request
The server code is correct; the fix requires the client to send valid JSON with the proper Content-Type header to avoid JSON decode errors.

Workaround

Add a middleware or exception handler to catch RequestValidationError, log the raw request body for debugging, and return a clear error message to the client.

Prevention

Use client-side JSON validation and always set Content-Type to 'application/json' when sending JSON payloads to FastAPI endpoints.

Python 3.7+ · fastapi >=0.60.0 · tested on 0.95.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.