python
runtime_error
ai_generated
true
RuntimeError: Stream consumed
ID: python/starlette-middleware-body-consumed
80%Fix Rate
83%Confidence
0Evidence
2024-12-04First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
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.
generic中文
自定义中间件通过 await request.body() 读取了请求体,下游处理程序再次读取;Starlette 流只能消费一次。
Workarounds
-
85% success
In middleware: body = await request.body(); request._body = body (cache it)
-
90% success
Use request.state to pass parsed data instead of re-reading body
Dead Ends
Common approaches that don't work:
-
90% fail
Second call raises Stream consumed.
-
75% fail
Handler receives empty body and fails validation.