0

i have view for editing user profile with ajax, i'm checking the request type, if its post just get Json data with request.body. but Django is giving me an error: 'You cannot access body after reading from request's data stream'. apparently i can only access request data one, so how do i check request type before reading the body. here is simplified code

def edit_profile(request):
    if request.method == 'POST':
        name = ''
        email = ''
        bio = ''
        user_profile = User.objects.get(username=request.user.username)

        try:
            req_str = request.body.decode('utf-8')
            req_json = json.loads(req_str)

            name = req_json['username']
            email = req_json['email']
            bio = req_json['bio']

            if name:
                user_profile.username = name
            if email:
                user_profile.email = email
            if bio:
                user_profile.account.bio = bio

            user_profile.save()
            user_profile.account.save()
        except KeyError:
            print("can't parse json")
        except Exception as e:
            print('fialed to proccess request', e)

        return JsonResponse({
            'username': user_profile.username,
            'email': user_profile.email,
            'bio': user_profile.account.bio
        })
    else:
        return HttpResponse('nothing changed')

full error message:

Internal Server Error: /accounts/edit_profile/
Traceback (most recent call last):
  File "/home/void/Projects/workspace/venv/lib/python3.4/site-packages/django/core/handlers/exception.py", line 42, in inner
    response = get_response(request)
  File "/home/void/Projects/workspace/venv/lib/python3.4/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/void/Projects/workspace/venv/lib/python3.4/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/void/Projects/workspace/workspace/accounts/views.py", line 79, in edit_profile
    req_str = request.body.decode('utf-8')
  File "/home/void/Projects/workspace/venv/lib/python3.4/site-packages/django/http/request.py", line 264, in body
    raise RawPostDataException("You cannot access body after reading from request's data stream")
django.http.request.RawPostDataException: You cannot access body after reading from request's data stream
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
void
  • 73
  • 2
  • 10
  • That code wouldn't even work (you refer to different variables in lines 3 and 4) but if it did, it wouldn't cause the error you claim: checking the request method does not "read" the request. Please show the *actual* code you used and the full error you got. – Daniel Roseman Apr 08 '17 at 18:28
  • 1
    Where is the full error and traceback? – Daniel Roseman Apr 08 '17 at 18:38
  • The suspicious thing is that this exception should have been caught by the `except Exception` line in your view. Still, do you have any custom middleware that might be interfering? – Daniel Roseman Apr 08 '17 at 19:26

1 Answers1

0

Your middleware must be accessing the request.body which might be causing the error, please examine your MIDDLEWARE_CLASSES for any custom/middlewares that might be accessing the data. Check if anyone of the module/app which you are using in middleware has this bug.

There is some good explanation here. https://stackoverflow.com/a/28641930/3448851

Community
  • 1
  • 1
rrmerugu
  • 1,826
  • 2
  • 20
  • 26