This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author drueter@assyst.com
Recipients drueter@assyst.com
Date 2015-06-17.06:48:39
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1434523722.13.0.839663684524.issue24460@psf.upfronthosting.co.za>
In-reply-to
Content
In Python 3.4 I would like to serialize a dictionary into a URL-encoded string.

Given a dictionary like this: 

>>> thisDict = {'SomeVar1': [b'abc'], 'SomeVar2': [b'def'], 'SomeVar3': [b'ghi']}

I would like to be able to return this string:

	SomeVar1=abc&SomeVar2=def&SomeVar3=ghi

I thought that urllib.parse.urlencode would work for me, but it does not:

>>> print(urllib.parse.urlencode(thisDict))
	SomeVar1=%5Bb%27abc%27%5D&SomeVar2=%5Bb%27def%27%5D&SomeVar3=%5Bb%27ghi%27%5D

In other words, urlencode on the dictionary is performing a URL encode on the string that is returned when the dictionary is cast to a string...and is including the square brackets (escaped) and the byte literal "b" indicator.

{'SomeVar1': [b'abc'], 'SomeVar2': [b'def'], 'SomeVar3': [b'ghi']}

I can obtain the desired string with this:

>>> '&'.join("{!s}={!s}".format(key,urllib.parse.quote_plus(str(val[0],'utf-8'))) for (key,val) in thisDict.items())

Is the behavior of urllib.parse.urlencode() on a dictionary intentional?  When would the current behavior ever be useful?

Would it make sense to change the behavior of urllib.parse.urlencode such that it works as described above?
History
Date User Action Args
2015-06-17 06:48:42drueter@assyst.comsetrecipients: + drueter@assyst.com
2015-06-17 06:48:42drueter@assyst.comsetmessageid: <1434523722.13.0.839663684524.issue24460@psf.upfronthosting.co.za>
2015-06-17 06:48:42drueter@assyst.comlinkissue24460 messages
2015-06-17 06:48:39drueter@assyst.comcreate