Convert dictionary to HTTP POST

Sybren Stuvel sybrenUSE at YOURthirdtower.com.imagination
Fri Mar 3 10:22:59 CET 2006


Laszlo Zsolt Nagy enlightened us with:
> How can I convert a dictionary into a HTTP POST string?  I have an
> example below, but this is not working correctly for special
> characters. (" ' and others).

URL-quote it.

> In other words, if I use "Bessy's cat" instead of "Bessy" then the
> http server will parse that to "Bessy's cat"

The HTTP server won't perform HTML-quoting out of thin air.

> Probably the problem is that I should not use urllib.quote but
> something else.

URL-quoting is the right thing to do:

In [1]: import urllib

In [2]: urllib.quote("Bessy's cat")
Out[2]: 'Bessy%27s%20cat'

If you want to give yourself an example of how it's done, run a
network sniffer and post something to a website.

Sybren
-- 
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself? 
                                             Frank Zappa



More information about the Python-list mailing list