http POST question

  • Thread starter Raaijmakers, Vincent (GE Infrastructure)
  • Start date
R

Raaijmakers, Vincent (GE Infrastructure)

Please tell me if this is true or not..

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

Until yesterday when I needed a bare bone BaseHTTPServer, creating my own do_POST:

def do_POST(self):
print self.path

there were no spam, bacon and eggs in "self.path". They are only there when I change conn.request in:
conn.request("POST", "/cgi-bin/query?spam=1&eggs=2&bacon=0", params, headers)

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.

So, is it indeed true that I misunderstood the formatting?

Thanks,
Vincent
 
C

Christopher T King

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.
 
A

Andrea Griffini

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

You should read just "content-length" bytes because otherwise you
may get stuck waiting forever.

Andrea
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top