0
votes

I'm having a hard time finding the cause of this. I have a heartbeat view with token authentication, it just returns status=200 and I'm getting the response content must be rendered before it can be iterated over error.

It's related to token authentication but for the life of me, I can't figure it out.

urlpatterns = [
    path('heartbeat/', views.HeartbeatView.as_view(), name='heartbeat')]


class TokenAuthentication(authentication.BaseAuthentication):
    def authenticate(self, request):
        auth_token = request.META.get('HTTP_AUTHTOKEN')
        if not auth_token:
            return Response('No token', status=450)
        try:
            auth_token_inst = AuthToken.objects.select_related('user').get(token=auth_token)
            if not auth_token_inst:
                return Response('Not a valid token', status=451)
            if auth_token_inst.is_active is False:
                return Response('Expired token', status=452)
            user = auth_token_inst.user
            auth_token_inst.ExtendExpireDate()
        except AuthToken.DoesNotExist:
            return Response('No token', status=450)

        return (user, None)

class HeartbeatView(APIView):
    authentication_classes = (TokenAuthentication,)

    def get(self, request):
        """
        Update token with heartbeat
        """
        return HttpResponse(status=200)

[15/Jul/2019 07:10:31] ERROR [django.request:228] Internal Server Error: /heartbeat/ Traceback (most recent call last): File "/home/ubuntu/virtenv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/home/ubuntu/virtenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "/home/ubuntu/virtenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/ubuntu/virtenv/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "/home/ubuntu/virtenv/lib/python3.5/site-packages/django/views/generic/base.py", line 71, in view return self.dispatch(request, *args, **kwargs) File "/home/ubuntu/virtenv/lib/python3.5/site-packages/rest_framework/views.py", line 495, in dispatch response = self.handle_exception(exc) File "/home/ubuntu/virtenv/lib/python3.5/site-packages/rest_framework/views.py", line 455, in handle_exception self.raise_uncaught_exception(exc) File "/home/ubuntu/virtenv/lib/python3.5/site-packages/rest_framework/views.py", line 483, in dispatch self.initial(request, *args, **kwargs) File "/home/ubuntu/virtenv/lib/python3.5/site-packages/sentry_sdk/integrations/django/init.py", line 264, in sentry_patched_drf_initial return old_drf_initial(self, request, *args, **kwargs) File "/home/ubuntu/virtenv/lib/python3.5/site-packages/rest_framework/views.py", line 400, in initial self.perform_authentication(request) File "/home/ubuntu/virtenv/lib/python3.5/site-packages/rest_framework/views.py", line 326, in perform_authentication request.user File "/home/ubuntu/virtenv/lib/python3.5/site-packages/rest_framework/request.py", line 223, in user self._authenticate() File "/home/ubuntu/virtenv/lib/python3.5/site-packages/rest_framework/request.py", line 383, in _authenticate self.user, self.auth = user_auth_tuple File "/home/ubuntu/virtenv/lib/python3.5/site-packages/django/template/response.py", line 120, in iter 'The response content must be rendered before it can be iterated over.' django.template.response.ContentNotRenderedError: The response content must be rendered before it can be iterated over.

1
Does it work if you return HttpResponse('', status=200)?Dov Rine
Also, is this django.http.HttpResponse?Dov Rine
What @KhashayarGhamati said. ;)Dov Rine

1 Answers

0
votes

I've discovered that returning from authentication.BaseAuthentication's authenticate method is wrong. The best way is to raise an exception like this otherwise things get weird.

raise exceptions.AuthenticationFailed('Your message here')