Home > Net >  how to get access to post data incoming inside a Django middleware?
how to get access to post data incoming inside a Django middleware?

Time:11-26

I am writing a middleware due to which i need to access post data but none of the answers are solving my doubt, i am reading the data like this but this is working in postman but giving error in browser

  dict_str = request.body.decode("UTF-8")

any help is highly appreciated

error

RawPostDataException at /accounts/api/v1/register/
You cannot access body after reading from request's data stream

CodePudding user response:

You can get by reqeust.data, once POST body parsed on DRF, accessing to body is hindered.

CodePudding user response:

You can try this, i hope this will work

import json
import ast
def CustomMiddleware(get_response):
    def middleware(request):
        bytes_request_data = getattr(request, '_body', request.body)
        if bytes_request_data:
            request_data = ast.literal_eval(bytes_request_data.decode('utf-8'))
            print(request_data)

  • Related