问题描述

  在使用request.body获取raw里的Json时,如果你在获取的时候又直接对body作修改,那么会出现此错误!
官方解释为:
Accessing request.POST or request.REQUEST inside middleware from process_request or process_view will prevent any view
running after the middleware from being able to modify the upload handlers for the request, and should normally be avoided.

问题重现

代码如下:

re_request_body = eval(request.body.decode("utf-8"))

然后就会报错: You cannot access body after reading from request’s data stream
在这里插入图片描述

解决方法

方式一
将request.body复制一份给自定义的变量
修改代码为:

body_content = request.body
            re_request_body = eval(body_content.decode("utf-8"))

即可解决问题!因为复制一份出来的变量是独立的,与request.body没有关系,我们只需要修改Body_content里的内容就可以啦!

方式二

            body_content = request.data

使用request.data获取raw里的内容,而且获取到的内容直接为字典或者列表。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐