python runtime_error ai_generated true

RuntimeError: 流已被消费

RuntimeError: Stream consumed

ID: python/starlette-middleware-body-consumed

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. 85% 成功率
    In middleware: body = await request.body(); request._body = body (cache it)
  2. 90% 成功率
    Use request.state to pass parsed data instead of re-reading body

无效尝试

常见但无效的做法:

  1. 90% 失败

    Second call raises Stream consumed.

  2. 75% 失败

    Handler receives empty body and fails validation.