python runtime_error ai_generated partial

运行时错误:无法读取请求体,请使用 stream 参数。

RuntimeError: Unable to read request body, please use the stream parameter.

ID: python/starlette-request-body-stream-error

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

版本兼容性

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

根因分析

尝试直接访问 request.body(),但请求体已经被消费。

English

尝试直接访问 request.body(),但请求体已经被消费。

generic

解决方案

  1. 85% 成功率
    async def body_middleware(request, call_next):
        body = await request.body()
        request._body = body
        response = await call_next(request)
        return response

无效尝试

常见但无效的做法:

  1. 90% 失败

    请求体只能读取一次,第二次会抛出异常

  2. 60% 失败

    stream 需要正确管理,否则会导致内存泄漏