Riziero
Riziero

Reputation: 144

Python - Making an HTTP POST request, why my payload ends up in form?

I am using urlib2 and http://httpbin.org/post(/get) to test my client.

When I go get mode

values = [('shelf' , 'XXXX'),('who' , 'YYYYY'])]
request_url = '%s?%s' %('http://httpbin.org/get, urllib.urlencode(values))
request = urllib2.Request(request_url)
print urllib2.urlopen(request_url).read()

I get the args i am passing in correctly and they do show up in args.

When I go post thoguh

values = [('shelf' , 'XXXX'),('who' , 'YYYYY'])]
request_url = 'http://httpbin.org/post'
request = urllib2.Request(to_url)
request.add_header('Content-Type', 'application/text')  
return urllib2.urlopen(request, urllib.urlencode(values))

on the server side args is empty and they end up in the data field:

{
    "args": {},
    "files": {},
    "data": {
        "shelf": "XXXX",
        "who": "YYYY"
      ....

On the server side I'd like to catch them no matter what I am using through args. Is this even possible? The reason I need this is that on the server side I am using http://twistedmatrix.com/documents/15.0.0/api/twisted.web.http.Request.html#process which only seems to expose args.

Cheers

Upvotes: 1

Views: 497

Answers (1)

Josh Kupershmidt
Josh Kupershmidt

Reputation: 2710

If I understand your question correctly, you would like for your POST to the server to have your parameters passed in as part of the query string. That is pretty simple: just tack them on in the request_url the same way as you have done in your GET request. Here is a (somewhat fixed-up, now actually functional and self-contained) working example from your POST snippet:

import urllib
import urllib2

values = [('shelf' , 'XXXX'),('who' , 'YYYYY')]
request_url = '%s?%s' % ('http://httpbin.org/post', urllib.urlencode(values))
request = urllib2.Request(request_url)
request.add_header('Content-Type', 'application/text')
req = urllib2.urlopen(request, urllib.urlencode(values))
print req.read()

Note, now 'shelf' and 'who' are being sent to your server in two places, in both the query string and the POST body. This may or may not be what you really want. If you don't really need these variables sent in the POST body, you could change the req = ... line to be req = urllib2.urlopen(request, '').

Upvotes: 1

Related Questions