django post请求报错: You cannot access body after reading from request‘s data stream
·
问题描述
在使用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里的内容,而且获取到的内容直接为字典或者列表。
更多推荐
所有评论(0)