Farhan Syedain
Farhan Syedain

Reputation: 528

django.http.request.RawPostDataException: You cannot access body after reading from request's data stream

I am making an API and i am gettings django.http.request.RawPostDataException: You cannot access body after reading from request's data stream this error I think You cant access data scnd time: Here is my code

def get_posts(request):
    print('Called')
    print(request.body)
    data = json.loads(request.body) #Error here
        
    user = data['user']
    print(user)
    
    user_profile = User.objects.all().get(username=user).profile
    
    posts = Post.objects.all().filter(user=user_profile)
    
    if len(posts) == 0:
        return JsonResponse({'last':True,'posts':None})
    
    
    if len(posts) < 4:
        
        data_ = PostSerilizer(data=posts,many=True)
        
        return Response(data=data_)
    
    
    
    else:
        deliverd_count = data['deliverd_count']
        
        posts_to_be_returned = posts[deliverd_count:deliverd_count+4]
        
        to_be_returned = {
            'posts':posts_to_be_returned,
            'last':False,
        }
        
        return JsonResponse(to_be_returned) 

How can i access data sceond time Here are the reasons for the error but i dont know how to solve When i am using api_view decorator -- which is necessary -- and it access data and since django allows you to acces data only one time, I am not able to access it

Upvotes: 0

Views: 905

Answers (1)

Farhan Syedain
Farhan Syedain

Reputation: 528

I now got the answer. You can say data = request.data ,it is same .

Upvotes: 0

Related Questions