python runtime_error ai_generated true

django.http.request.RawPostDataException: You cannot access body after reading from request's data stream

ID: python/django-request-data-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
0Evidence
2025-06-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.2 active
4.0 active
5.0 active

Root Cause

多次尝试读取请求体,但请求体只能读取一次

generic

中文

Django 请求对象不允许重复读取 body 数据

Workarounds

  1. 95% success 一次性读取并缓存
    data = request.body; 之后使用 data 变量
  2. 90% success 使用中间件缓存请求体
    使用 django.utils.datastructures.MultiValueDictKeyError 处理

Dead Ends

Common approaches that don't work:

  1. 尝试重新读取 request.body 100% fail

    请求体流已消耗,无法再次读取

  2. 使用 request.POST 代替 60% fail

    如果请求是 JSON,request.POST 可能为空