python
data_error
ai_generated
true
422 Unprocessable Entity: {"detail":[{"type":"missing","loc":["body","email"],"msg":"Field required"}]}
ID: python/fastapi-422-missing-body
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.
解决方案
-
90% 成功率
class UserIn(BaseModel): email: EmailStr name: str = '' -
85% 成功率
from fastapi import Form @app.post('/u') async def create(email: str = Form(...)): ...
无效尝试
常见但无效的做法:
-
85% 失败
Hides the real cause; clients still send invalid payloads and data is dropped.
-
70% 失败
Still required in Pydantic v2 unless a default is provided.