473,379 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Convert dictionary to HTTP POST


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

Mar 3 '06 #1
1 2013
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
Mar 3 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Byron | last post by:
Hello, I am a newbie and would like to know if it is possible to convert a string back to a dictionary? For example, I can convert a dictionary to a string by doing this: >>> names =...
9
by: What-a-Tool | last post by:
Dim MyMsg Set MyMsg = server.createObject("Scripting.Dictionary") MyMsg.Add "KeyVal1", "My Message1" MyMsg.Add "KeyVal2", "My Message2" MyMsg.Add "KeyVal3", "My Message3" for i = 1 To...
5
by: aks1232001 | last post by:
Hello Everyone, I'm new to this place. Have a few doubts hoping to get some response soon :) 1. How can i convert a color BMP file into a monochrome black and white image (NOT GRAYSCALE) in C...
7
by: manstey | last post by:
Hi, How do I convert a string like: a="{'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS': u'8'}" into a dictionary: b={'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS':...
2
by: Tom Grove | last post by:
I have a server program that I am writing an interface to and it returns data in a perl dictionary. Is there a nice way to convert this to something useful in Python? Here is some sample data:...
18
by: Marko.Cain.23 | last post by:
Hi, I create a dictionary like this myDict = {} and I add entry like this: myDict = 1 but how can I empty the whole dictionary? Thank you.
35
by: Abandoned | last post by:
I want to convert a string to command.. For example i have a string: a="" I want to do this list.. How can i do ?
4
by: DippyDog | last post by:
I have a list of key-value pairs that should be shared, but I don't know how to code it. The trick for me is how to populate it when it's instantiated. Herfried got so close to showing me how in...
7
by: neeru29 | last post by:
I have new to python programming. I have sucessfully converted a dictionary into a string. but i want to know how can convert the string back to the dictionary. let us say that 'd' is...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.