python runtime_error ai_generated partial

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

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
80%Confidence
0Evidence
2024-08-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 90% fail

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

  2. 60% fail

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