Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception: You cannot access body after reading from request's data stream

Since Django 1.5 raw post data is accessible via request.body.

In my application I sometimes get data send via a form and sometimes raw data (json for example). Is there any way to write a function like this that does not fail?

def get_post_var(request, name):     result = request.POST.get(name)     if result:         return result      post_body = dict(urlparse.parse_qsl(request.body))     result = post_body.get(name)     if result:         return result      return None 
like image 211
kev Avatar asked Oct 25 '13 04:10

kev


1 Answers

Use request.data instead of request.body.

request.data does not read the data stream again.

like image 114
Arnab Biswas Avatar answered Sep 20 '22 21:09

Arnab Biswas