python data_error ai_generated true

fastapi.exceptions.RequestValidationError: [{'type': 'missing', 'loc': ['body'], 'msg': 'Field required', 'input': None}]

ID: python/fastapi-missing-body

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-10-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

The request body is required but was not sent. This often happens when a POST request is sent without a body or with wrong Content-Type.

generic

中文

请求体是必需的但未发送。这通常发生在 POST 请求没有发送请求体或 Content-Type 错误时。

Workarounds

  1. 95% success Send a valid JSON body with required fields
    curl -X POST http://localhost:8000/items -H 'Content-Type: application/json' -d '{"name": "item"}'
  2. 90% success Make the body optional in the endpoint
    from fastapi import Body
    @app.post('/items')
    def create_item(data: dict = Body(None)):
        return data

Dead Ends

Common approaches that don't work:

  1. Setting Content-Type to text/plain 80% fail

    FastAPI expects JSON by default.

  2. Sending an empty body 90% fail

    Empty body is still missing required fields.