Json.loads (request.body) gives error "No JSON object can be decoded" (Django)
I am creating an app using Django with Django Rest Framework and AngularJS with CoffeScript. I am based on the tutorial https://thinkster.io/brewer/angular-django-tutorial/ . I have a problem passing json from the client side, but I think the problem is clearly in my opinion, because even posting json via DRF api gives the same error - "No JSON object can be decoded" caused by the line data = json.loads(request.body)
.
My APIView is the following (clearly the same as in the tutorial):
class LoginView(views.APIView):
def post(self, request, format=None):
data = json.loads(request.body)
email = data.get('email', None)
password = data.get('password', None)
account = authenticate(email=email, password=password)
if account is not None:
if account.is_active:
login(request, account)
serialized = AccountSerializer(account)
return Response(serialized.data)
else:
return Response({
'status': 'Unathorized',
'message': 'This account has been disabled.'
}, status=status.HTTP_401_UNATHORIZED)
else:
return Response({
'status': 'Unathorized',
'message': 'Username/password combination invalid.'
}, status=status.HTTP_401_UNATHORIZED)
When I try to pass JSON from api and try to print request.body in the console, I have a result like this:
csrfmiddlewaretoken=s2dIiOy7eNUJJfGBRaDAFmJ632kjbokz&_content_type=application%2Fjson&_content=%7B%0D%0A%22email%22%3A+%22example@ex.com%22%2C%0D%0A%22password%22%3A+%22example%22%0D%0A%7
It doesn't seem like valid JSON. Am I correct that there is something wrong with this view? Or should I be looking elsewhere?
source to share