gxmad
gxmad

Reputation: 2220

How can I get my list passed by url?

My problem is how to get my list back. The original is:

["tick", "tac", "too"]

I send it in the get response to my server, then I get this encoded url:

patient%5B%5D=tick&patient%5B%5D=tac&patient%5B%5D=too

After applying parse, I get the result bellow:

urllib.parse.unquote(f.url)
>> /?patient[]=tick&patient[]=tac&patient[]=too

But It is still not an array, does someone know a simple way to get my list from this url?

Upvotes: 2

Views: 39

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476813

I think you better here use a library to decodes the query string.

You can use the function parse_qs from urlib.parse:

>>> urllib.parse.parse_qs('patient%5B%5D=tick&patient%5B%5D=tac&patient%5B%5D=too')
{'patient[]': ['tick', 'tac', 'too']}

So you can write:

from urllib.parse import parse_qs

tictactoo = urllib.parse.parse_qs('patient%5B%5D=tick&patient%5B%5D=tac&patient%5B%5D=too')['patient[]']

then tictactoo is:

>>> tictactoo
['tick', 'tac', 'too']

Upvotes: 2

Related Questions