python data_error ai_generated true

422 Unprocessable Entity: {"detail":[{"type":"missing","loc":["body","email"],"msg":"Field required"}]}

ID: python/fastapi-422-missing-body

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

版本兼容性

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

根因分析

客户端发送的 JSON 正文缺少必填字段,或发送了表单数据而期望 JSON,导致 Pydantic 验证拒绝请求。

English

The client sent a JSON body missing a required field, or sent form data where JSON was expected, so Pydantic validation rejects the request.

generic

解决方案

  1. 90% 成功率
    class UserIn(BaseModel):
        email: EmailStr
        name: str = ''
  2. 85% 成功率
    from fastapi import Form
    @app.post('/u')
    async def create(email: str = Form(...)): ...

无效尝试

常见但无效的做法:

  1. 85% 失败

    Hides the real cause; clients still send invalid payloads and data is dropped.

  2. 70% 失败

    Still required in Pydantic v2 unless a default is provided.