Saturday, April 23, 2016

Re: Can not read request.body from a POST request

Error says : You cannot access body after reading from request's data stream and CsrfViewMiddleware is accessing before the post request. I disabled it and it worked but this is not a safe solution.
Django docs confirm that:

Accessing request.POST 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.

The CsrfViewMiddleware class can be considered an exception, as it provides the csrf_exempt() and csrf_protect() decorators which allow views to explicitly control at what point the CSRF validation should occur.


https://docs.djangoproject.com/en/dev/topics/http/middleware/


So request.body could not be read without disabling csrf middleware.
I attached an example with that situation.
Thanks.
On Saturday, April 23, 2016 at 5:00:45 PM UTC+3, Daniel Roseman wrote:
On Wednesday, 20 April 2016 21:29:09 UTC+1, Mihai Corciu wrote:
Is there a method to access request.body from a POST request in Django, without disabling 'django.middleware.csrf.CsrfViewMiddleware' ?

CsrfViewMiddleware is causing:

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

which means request.body is impossible to acces without disabling CsrfViewMiddleware.


This should not be happening. CsrfViewMiddleware is enabled by default and does not interfere with reading request.body. Please post the full traceback.
--
Daniel.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/84eb2af2-7e34-48c0-b33e-6c754d84cdb3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment