Forums

Problem with rest framework apiview.

Hello I have a APIView that have a post function that response to login data. this code is in my own system that run correctly.

class LoginApi(views.APIView):    
    permission_classes = (AllowAny,) # Adding permission to everyone
    def post(self,request):        
        email = request.data['email']
        password = request.data['password']
        ...

But when send this code to pythonanywhere it handle MultiValueDictKeyError. it seems that the shape pf request.data change to something like this:

 <QueryDict: {'_content_type': ['application/json'], '_content': ['{"email":"email address","password":"pass"}']}>

It must be like this:

 {"email":"email address","password":"pass"}

actually it's take to time to change every post function in project. my django and rest framework version is the same with my own system. just my python version is not the same . my system is 3.10 and pythonanywhere is 3.9 but i think its not the problem

What is the whole error?

it says 'email' key not exist.

you can send post request to login in the shape {"email":"EMAIL", "password":" pass"} to see error.

You can add some logging to see what is actually coming to the view that handles the request.

yes used logging to see what's happening.

the request.data is like this:

 <QueryDict: {'_content_type': ['application/json'], '_content': ['{"email":"email address","password":"pass"}']}>

but in my system is this :

 {"email":"email address","password":"pass"}

Could that be related to how parsers are being chosen by Django rest api (see the docs)?