python data_error ai_generated true

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

ID: python/starlette-request-body-parse-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2025-06-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.10 active
3.11 active

Root Cause

Starlette 收到的请求体不是有效的 JSON 格式

generic

中文

Starlette 收到的请求体不是有效的 JSON 格式

Workarounds

  1. 95% success 验证请求体格式并返回错误
    try:
        data = await request.json()
    except JSONDecodeError:
        return JSONResponse({'error': '无效的 JSON'}, status_code=400)
  2. 90% success 使用 Pydantic 模型自动验证
    from pydantic import BaseModel
    class Item(BaseModel):
        name: str
    item = Item.parse_raw(await request.body())

Dead Ends

Common approaches that don't work:

  1. 忽略错误并返回空数据 80% fail

    可能接受无效请求

  2. 使用 eval 解析请求体 95% fail

    存在严重安全风险