0

I have this payload:

            "cardType": "CHOICE",
            "step": 40,
            "title": {"en-GB": "YOUR REPORT"},
            "description": {"en-GB": ""},
            "options": [
                {"optionId": 0, "text": {"en-GB": "Ask me"}},
                {"optionId": 1, "text": {"en-GB": "Phone a nurse"}},
                {"optionId": 2, "text": {"en-GB": "Download full report"}},
            ],
            "_links": {
                "self": {
                    "method": "GET",
                    "href": "/assessments/898d915e-229f-48f2-9b98-cfd760ba8965",
                },
                "report": {
                    "method": "GET",
                    "href": "/reports/17340f51604cb35bd2c6b7b9b16f3aec",
                },
            },
        }

I then url encode it like so and redirect to a report view:

url = reverse("my-reports")
reverse_url = encodeurl(data, url)

The urlencode output is returned as this:

"/api/v2/ada/reports?cardType=CHOICE&step=40&title="
        "%7B%27en-GB%27%3A+%27YOUR+REPORT%27%7D&description="
        "%7B%27en-GB%27%3A+%27%27%7D&options="
        "%5B%7B%27optionId%27%3A+0%2C+%27text%27%3A+%7B%27en-GB"
        "%27%3A+%27Ask+me%27%7D%7D%2C+%7B%27optionId%27%"
        "3A+1%2C+%27text%27%3A+%7B%27en-GB%27%3A+%27Phone+a+nurse"
        "%27%7D%7D%2C+%7B%27optionId%27%3A+2%2C+%27text%27%3A+%7B"
        "%27en-GB%27%3A+%27Download+full+report%27%7D%7D%5D&_links="
        "%7B%27self%27%3A+%7B%27method%27%3A+%27GET%27%2C+%27href%27%"
        "3A+%27%2Fassessments%2F898d915e-229f-48f2-9b98-cfd760ba8965"
        "%27%7D%2C+%27report%27%3A+%7B%27method%27%3A+%27GET%27%"
        "2C+%27href%27%3A+%27%2Freports%2F"
        "17340f51604cb35bd2c6b7b9b16f3aec%27%7D%7D"

In my report view I get the payload from the url query string:

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

    def get(self, request, *args, **kwargs):
        result = request.GET
        data = result.dict()
        print(data)

Now the problem is that the output of the transferred data which is printed above has quotes for nested keys.

{
    'cardType': 'CHOICE', 
    'step': '40', 'title': "{'en-GB': 'YOUR REPORT'}", 
    'description': "{'en-GB': ''}", 
    'options': "[{'optionId': 0, 
    'text': {'en-GB': 'Ask me'}}, 
    {'optionId': 1, 'text': {'en-GB': 'Phone a nurse'}}, {'optionId': 2, 'text': {'en-GB': 'Download full report'}}]", 
    '_links': "{'self': {'method': 'GET', 'href': '/assessments/898d915e-229f-48f2-9b98-cfd760ba8965'}, 'report': {'method': 'GET', 'href': '/reports/17340f51604cb35bd2c6b7b9b16f3aec'}}"
}

Notice the double quote in the "description", "options" keys etc. Is there a way to remove this or pass this across to the view so that when I unwrap it I have the same data I started with.

Thanks

user2875230
  • 195
  • 1
  • 2
  • 14
  • 1
    Does this answer your question? [Convert a String representation of a Dictionary to a dictionary?](https://stackoverflow.com/questions/988228/convert-a-string-representation-of-a-dictionary-to-a-dictionary) – Arkistarvh Kltzuonstev May 30 '22 at 17:59
  • Unfortunately it doesn't since my data is a dictionary not a string. – user2875230 May 30 '22 at 18:09

1 Answers1

1
import urllib.parse
import json

# convert your dict to a string
json_string = json.dumps(your_payload)

# urlencode the json string
encoded = urllib.parse.quote(json_string.encode('utf-8'))

# build the url
url = reverse("my-reports")
reverse_url = f"{url}?payload={encoded}"

Then in your view:

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

    def get(self, request, *args, **kwargs):
        payload = request.GET.get('payload')
        
        # do the encoding process in reverse
        urldecoded = urllib.parse.unquote(payload)
        data = json_loads(urldecoded)
        print(data)
lucutzu33
  • 3,470
  • 1
  • 10
  • 24