Convert dictionary to HTTP POST

Laszlo Zsolt Nagy gandalf at designaproduct.biz
Fri Mar 3 04:11:41 EST 2006


  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()       




More information about the Python-list mailing list