Error with urlencode in python Error with urlencode in python python python

Error with urlencode in python


The urlencode library expects data in str format, and doesn't deal well with Unicode data since it doesn't provide a way to specify an encoding. Try this instead:

mp3_data = {'album': u'Metamorphine',     'group': 'monoku',     'name': u'Son Of Venus (Danny\xb4s Song)',     'artist': u'Leandra',     'checksum': '2836e33d42baf947e8c8adef48921f2f76fcb37eea9c50b0b59d7651',     'track_number': 8,     'year': '2008', 'genre': 'Darkwave',     'path': u'/media/data/musik/Leandra/2008. Metamorphine/08. Son Of Venus (Danny\xb4s Song).mp3',     'user_email': 'diegueus9@gmail.com',     'size': 6624104}str_mp3_data = {}for k, v in mp3_data.iteritems():    str_mp3_data[k] = unicode(v).encode('utf-8')data = urllib.urlencode(str_mp3_data)

What I did was ensure that all data is encoded into str using UTF-8 before passing the dictionary into the urlencode function.


If you are using Django, take a look at Django's QueryDict class, it has a urlencode() method.

Or, for the helper function itself you may use urlencode. It basically does what is described in the other answers as a wrapper around the original urllib.encode.


The problem is that some of the values in your mp3_data dict are unicode strings that can't be represented in the default encoding used by urlencode() (while others are ASCII and still others are integers). You can fix this by encoding those values before passing them to urlencode(). On line 14 of /home/diegueus9/workspace/playku/src/client/playkud/service.py, in make_request(), try changing this:

data = urllib.urlencode(dict([k.encode('utf-8'),v] for k,v in mp3_data.items()))

to this:

data = urllib.urlencode(dict([k.encode('utf-8'),unicode(v).encode('utf-8')] for k,v in mp3_data.items()))