问题描述

  在使用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

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐