Home > other >  TypeError: Object of type 'HttpResponseRedirect' is not JSON serializable
TypeError: Object of type 'HttpResponseRedirect' is not JSON serializable

Time:04-15

I'm trying to run a simple app which receives a payload from an external application and enters EntryLayerView. This view calls a method in utils.py which then redirects the payload to another view for processing. However, I keep seeing this not Json serializable error.

Url.py

path(
        "api/v2/myapp/assessments",
        views.EntryLayerView.as_view(),
        name="myapp-assessments",
    ),
    path(
        "api/v2/myapp/startconversation",
        views.startconversation.as_view(),
        name="myapp-start-assessment",
    ),

Views.py The entry point to the app is the EntryLayerView

class EntryLayerView(generics.GenericAPIView):
    permission_classes = (permissions.IsAuthenticated,)

    def post(self, request, *args, **kwargs):
        body = request.data
        response = get_endpoint(validated_body) #Don't worry about this line
        return Response(response, status=status.HTTP_200_OK)


class startconversation(generics.GenericAPIView):
    permission_classes = (permissions.AllowAny,)

    def post(self, request, *args, **kwargs):
        print("I'm inside the view")
        redirect = request.GET.get('all the data')
        #This is the view I'm trying to pass data to
        

utils.py

def get_endpoint(payload):
    qs = '?'  urlencode(payload)
    reverse_url = reverse('myapp-start-assessment')
    url = reverse_url   qs
    print(url)
    return HttpResponseRedirect(url)

The output of url in utils.py is:

/api/v2/myapp/startconversation?contact_uuid=67460e74-02e3-11e8-b443-00163e990bdb&choices=None&value=&cardType=&step=None&optionId=None&path=&title=&description=&message=

Error:

    return json.dumps(*args, **kwargs)
  File "/usr/local/lib/python3.6/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/usr/local/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/local/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/home/osboxes/ndoh-hub/venv/lib/python3.6/site-packages/rest_framework/utils/encoders.py", line 67, in default
    return super().default(obj)
  File "/usr/local/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'HttpResponseRedirect' is not JSON serializable

I'm unable to understand why I'm seeing this JSON not serializable error. It's the same issue if I use JsonResponse or redirect.

CodePudding user response:

Try to change a little your code:

def get_endpoint(payload):
    qs = '?'  urlencode(payload)
    reverse_url = reverse('myapp-start-assessment')
    url = reverse_url   qs
    print(url)
    return url  # return url not Response

And then in your EntryLayerView:

def post(self, request, *args, **kwargs):
    body = request.data
    url = get_endpoint(validated_body) #Don't worry about this line
    return HttpResponseRedirect(url)

Also, don't use 2.. codes for redirect, use 3.. instead.

  • Related