python
runtime_error
ai_generated
true
RuntimeError: 流已被消费
RuntimeError: Stream consumed
ID: python/starlette-middleware-body-consumed
80%修复率
83%置信度
0证据数
2024-12-04首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
自定义中间件通过 await request.body() 读取了请求体,下游处理程序再次读取;Starlette 流只能消费一次。
English
Custom middleware read the request body via await request.body() and downstream handler tried to read it again; Starlette streams can only be consumed once.
解决方案
-
85% 成功率
In middleware: body = await request.body(); request._body = body (cache it)
-
90% 成功率
Use request.state to pass parsed data instead of re-reading body
无效尝试
常见但无效的做法:
-
90% 失败
Second call raises Stream consumed.
-
75% 失败
Handler receives empty body and fails validation.