Laszlo Zsolt Nagy wrote: > Hello, > > 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). In other words, if I use "Bessy's cat" > instead of "Bessy" then the http server will parse that to "Bessy's cat" > Probably the problem is that I should not use urllib.quote but something > else. > Can you please advise? > > Laszlo > > form_values = {'name':'Bessy','age':'10','gender':'female'} > for key,value in form_values.iteritems(): > values.append('%s=%s' % (urllib.quote(key),urllib.quote(value)) ) > > values.append('x=33') > values.append('y=14') > post_data = ('&'.join(values)).replace('/','%2F') > txheaders = { > > 'Accept':'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5', > 'Accept-Language':'en,hu;q=0.8,en-us;q=0.5,hu-hu;q=0.3', > 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', > } > req = urllib2.Request(url, post_data, txheaders) > u = urllib2.build_opener() > req.add_data(post_data) > page2 = self.download(action,post_data,{ > 'Content-Type': 'application/x-www-form-urlencoded' > }) > openerdirector = u.open(req) > data = openerdirector.read() > See urllib.urlencode(). No idea why they don't include it in urllib2 as well, but there you go.
>>> from urllib import urlencode >>> urlencode({'a':'& "Simple string"', 'b': '<>[EMAIL PROTECTED]&*()_+='}) 'a=%26+%22Simple+string%22&b=%3C%3E%21%40%23%24%25%5E%26%2A%28%29_%2B%3D' >>> regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC/Ltd www.holdenweb.com Love me, love my blog holdenweb.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list