python runtime_error ai_generated true

fastapi.exceptions.RequestValidationError:值不是有效的字典 (type=type_error.dict)

fastapi.exceptions.RequestValidationError: value is not a valid dict (type=type_error.dict)

ID: python/fastapi-form-data-required

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2026-01-05首次发现

版本兼容性

版本状态引入弃用备注
0.100.x active
0.110.x active

根因分析

发送表单数据但端点期望 JSON,或反之。

English

Sending form data but endpoint expects JSON, or vice versa.

generic

解决方案

  1. 95% 成功率
    from fastapi import Form
    @app.post('/login')
    async def login(username: str = Form(...), password: str = Form(...)):
  2. 90% 成功率
    For JSON: `Content-Type: application/json`; for form: `application/x-www-form-urlencoded`.

无效尝试

常见但无效的做法:

  1. 60% 失败

    If server expects form, JSON still fails.

  2. 80% 失败

    Still expects JSON.