python - RawPostDataException: You cannot access body after reading from request's data stream -
i hosting site on google cloud , got work beautifully , of sudden start getting error..
01:16:22.222 internal server error: /api/v1/auth/login/ (/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/django/core/handlers/exception.py:124) traceback (most recent call last): file "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/django/core/handlers/exception.py", line 39, in inner response = get_response(request) file "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/django/core/handlers/base.py", line 249, in _legacy_get_response response = self._get_response(request) file "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/django/core/handlers/base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) file "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/django/core/handlers/base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) file "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/django/views/decorators/csrf.py", line 58, in wrapped_view return view_func(*args, **kwargs) file "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/django/views/generic/base.py", line 68, in view return self.dispatch(request, *args, **kwargs) file "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/rest_framework/views.py", line 474, in dispatch response = self.handle_exception(exc) file "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/rest_framework/views.py", line 434, in handle_exception self.raise_uncaught_exception(exc) file "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/rest_framework/views.py", line 471, in dispatch response = handler(request, *args, **kwargs) file "/base/data/home/apps/s~crs-portal/1.395605052160854207/authentication/views.py", line 42, in post data = json.loads(request.body) file "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/rest_framework/request.py", line 359, in __getattribute__ return getattr(self._request, attr) file "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/django/http/request.py", line 263, in body raise rawpostdataexception("you cannot access body after reading request's data stream") rawpostdataexception: cannot access body after reading request's data stream
i have no clue means, , endless googling has not solved case in anyway..
here's code relevant:
views.py
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 not none: if account.is_active: login(request, account) serialized = accountserializer(account) return response(serialized.data) else: return response({ 'status': 'unauthorized', 'message': 'this account has been disabled.' }, status=status.http_401_unauthorized) else: return response({ 'status': 'unauthorized', 'message': 'username/password combination invalid.' }, status=status.http_401_unauthorized)
serializer.py
class accountserializer(serializers.modelserializer): password = serializers.charfield(write_only=true, required=false) confirm_password = serializers.charfield(write_only=true, required=false) class meta: model = account fields = ('id', 'email', 'username', 'created_at', 'updated_at', 'full_name', 'password', 'confirm_password') read_only_fields = ('created_at', 'updated_at',) def create(self, validated_data): return account.objects.create(**validated_data) def update(self, instance, validated_data): instance.username = validated_data.get('username', instance.username) instance.save() password = validated_data.get('password', none) confirm_password = validated_data.get('confirm_password', none) if password , confirm_password , password == confirm_password: instance.set_password(password) instance.save() update_session_auth_hash(self.context.get('request'), instance) return instance
this occuring because trying access data body
use -> data = json.loads(request.data)
Comments
Post a Comment