# FastAPI请求验证错误：缺少文件字段

- **ID:** `python/fastapi-file-upload-empty`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

端点期望通过`UploadFile`上传文件，但请求的multipart表单数据中没有包含文件部分。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   Ensure the client sends multipart form data with the correct field name. For testing: `client.post('/upload', files={'file': ('filename.txt', b'content', 'text/plain')})`.
   ```
2. **** (90% 成功率)
   ```
   Make the file parameter optional: `file: UploadFile = File(None)`.
   ```

## 无效尝试

- **** — Sending the file as a JSON body doesn't work; FastAPI expects multipart form data. (95% 失败率)
- **** — Using `requests.post(url, files={'file': open('file.txt', 'rb')})` may fail if the field name doesn't match the parameter name. (80% 失败率)
