How to adjust the data in Django's WSGIRequest requests to make it also visible in the views?

19 views Asked by At

"I send an encrypted HTTP POST request, which is correctly received by the middleware on the Django server. The URL is successfully decrypted. However, I am facing difficulties in integrating the decrypted new data into the request so that it is readable in the views.

middleware.py

class DecryptMiddleware:

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        path = request.path
        if path.startswith('/'):
            path = path[1:]

        f = Fernet(settings.ENCRYPTION_KEY)
        decrypted_path = f.decrypt(path.encode()).decode()
        elements = decrypted_path.split('/')
        element = f"/{elements[0]}/"
        data = elements[1]

        jsondata = json.loads(unquote(data))

        query_dict = QueryDict('', mutable=True)
        for key, value in jsondata.items():
            query_dict.update({key: value})

        request.POST = query_dict
        request.method = 'POST'
        request.path = element
        request.path_info = element
        request.content_type = 'application/x-www-form-urlencoded'
        request.META['REQUEST_METHOD'] = 'POST'
        request.META['PATH_INFO'] = element
        request.META['QUERY_STRING'] = data
        request.META['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'
        request.META['CONTENT_LENGTH'] = str(len(request.body))

        response = self.get_response(request)
        return response

views.py

class CreateUserView(APIView):

    def post(self, request):
        print("success")
        serializer = UserSerializer()
        print(f"request.POST:{request. POST}")
        print(f"request.data:{request. Data}")
        if serializer.create(data=request.data) == False :
            return Response({ "info" : " identifiant error" }, status=status.HTTP_400_BAD_REQUEST) 
        return Response({ }, status=status.HTTP_200_OK) 

Console OUTPUT:

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
November 01, 2023 - 19:13:42
Django version 4.2.7, using settings 'APP.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

data:%7B%22username%22%3A%20%22XXXXXX%22%2C%20%22password%22%3A%20%22XXXXXX%22%2C%20%22email%22%3A%20%22XXXXXX%40XXXXXX.XXX%22%2C%20%22message%22%3A%20%22C%27XXXXXX
%20un%20XXXXXX%20de%20XXXXXX%22%7D
<QueryDict: {'username': ['XXXXXX'], 'password': ['XXXXXX'], 'email': ['[email protected]'], 'message': ["XXXXXX "]}>
success
request. POST:<QueryDict: {}>
request.data:<QueryDict: {}>
Bad Request: /create_user/
[01/Nov/2023 19:13:46] "POST /gAAAAABlQpVagCQkTohUKtiMXDBFDsx6p0Q06ytFfnQ0maeHHPpgsCFMpQ-zD_XVclellvc8JsWGqnACtlyYjPWRg__rwMmW2qJqmVMQFzn047OYb1B4ntyOkXYSjpv1B3frXNOdqXkPeuK7tZePI7rAdpWUPK-XUvNWHAxpiAyPWpWSoJt86vUMoDbCWC7zMUKyfOdoG6WV_yc2vMsRangold39FkzvFPBz2VXze87C6dw6byi1lYmAueXqEoYpe148LOe9Nm_EyEEYe52pH2rYnRbzOyRcb4n3qMuwporYF8EHeWBfVraPP1Mtd9R5syNpGeoiUf4Pyi6_sZQk--qiMJmsjp73HjD2SkaQkwg1jOzHiCO1gyc= HTTP/1.1" 400 29

I want to have this on views page :

request. POST:<QueryDict: {'username': ['value'], 'password': ['value'], 'email': ['value'], 'message': ['value']}>
request.data:<QueryDict: {'username': ['value'], 'password': ['value'], 'email': ['value'], 'message': ['value']}>
0

There are 0 answers