Search code examples
pythonpython-2.7urlencode

Python urlencode don't encode special characters


I'm forming a post request.

mydict = {'key1': 'value@1', 'key2': 'value@2'}
encoded_dict = urllib.urlencode(mydict)

This will result into

key1=value%401&key2=value%402

What I really want is

key1=value@1&key2=value@2

Any other way to this?


Solution

  • If you want to do your own thing, you'll have to write your own encoder. eg:

    mydict = {'key1': 'value@1', 'key2': 'value@2'}
    >>> "&".join("{}={}".format(*i) for i in mydict.items())
    'key2=value@2&key1=value@1'
    

    But why not just use JSON?