http POST question

Christopher T King squirrel at WPI.EDU
Wed Jul 14 10:38:29 EDT 2004


On Wed, 14 Jul 2004, Raaijmakers, Vincent (GE Infrastructure) wrote:

> In a all my applications I used the old fashioned way of "POST" requests:
> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
> headers = {"Content-type": "application/x-www-form-urlencoded"}
> conn = httplib.HTTPConnection("somelocation")
> conn.request("POST", "/cgi-bin/query", params, headers)
> 
> Now, without thinking.. have to admit.. I always thought that the
> request object would format this request into something like:
> /cgi-bin/query?spam=1&eggs=2&bacon=0

You're confusing POST with GET:

> Sniffing on the network learned me that my params are in the header, not
> in the post request and look like: spam=1&eggs=2&bacon.

The POST method places its parameters in the header of the request (just
after them, actually). To retrieve them in your do_POST() method, you 
have to read them from self.rfile (self.rfile.read() should do the 
trick).

If you'd rather pass the parameters in the URL, you have to use the GET 
method:

params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
conn = httplib.HTTPConnection("somelocation")
conn.request("GET", "/cgi-bin/query?" + params)

Then you can retrieve them in the do_GET() method of your server, using 
self.path.




More information about the Python-list mailing list