urlencode an array of values

Posted by Ikke on Stack Overflow See other posts from Stack Overflow or by Ikke
Published on 2010-04-03T11:41:54Z Indexed on 2010/04/03 11:53 UTC
Read the original article Hit count: 778

Filed under:
|

I'm trying to urlencode an dictionary in python with urllib.urlencode. The problem is, I have to encode an array.

The result needs to be:

criterias%5B%5D=member&criterias%5B%5D=issue
#unquoted: criterias[]=member&criterias[]=issue

But the result I get is:

criterias=%5B%27member%27%2C+%27issue%27%5D
#unquoted: criterias=['member',+'issue']

I have tried several things, but I can't seem to get the right result.

import urllib
criterias = ['member', 'issue']
params = {
    'criterias[]': criterias,
}
print urllib.urlencode(params)

If I use cgi.parse_qs to decode a correct query string, I get this as result:

{'criterias[]': ['member', 'issue']}

But if I encode that result, I get a wrong result back. Is there a way to produce the expected result?

© Stack Overflow or respective owner

Related posts about python

Related posts about http