python data_error ai_generated true

fastapi.exceptions.RequestValidationError: Body 有 1 个验证错误 body -> username 字段必填 (type=value_error.missing)

fastapi.exceptions.RequestValidationError: 1 validation error for Body body -> username field required (type=value_error.missing)

ID: python/fastapi-missing-form-data

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-10-15首次发现

版本兼容性

版本状态引入弃用备注
0.100+ active — — —
0.110+ active — — —

根因分析

端点声明了 Form(...) 但客户端发送 JSON,或声明了 JSON body 但客户端发送 form-urlencoded。

English

Endpoint declares Form(...) but client sends JSON, or declares JSON body but client sends form-urlencoded.

generic

解决方案

  1. 95% 成功率
    pip install python-multipart  # required for Form/File
  2. 90% 成功率
    async def login(username: str = Form(...), password: str = Form(...))  # and client must send application/x-www-form-urlencoded

无效尝试

常见但无效的做法:

  1. 80% 失败

    Server starts but Form parsing still fails at runtime.

  2. 65% 失败

    Breaks form clients; still 422 for the other content type.