Chris Dutrow Chris Dutrow - 1 year ago 83
Python Question

Transform a python dict into string compatible with Content-Type:"application/x-www-form-urlencoded"

I'd like to take a python dict object and transform it into its equivalent string if it were to be submitted as html form data.

The dict looks like this:


I believe the form string should look something like this:


What is a good way to do this?


Answer Source

Try urllib.parse.urlencode:

>>> from urllib.parse import urlencode
>>> urlencode({'foo': 1, 'bar': 2})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download