I am trying to access a web API using a POST technique. I AM able to access it using a GET technique, but the API owners tell me that certain functionality only works with POST. Unfortunately I can't seem to get POST working.
Here's what works with GET:
API_URL = "http://example.com/api/"
def call_api(method, **kwargs):
url = API_URL + method
if kwargs:
url += '?' + urllib.urlencode(kwargs)
req = urllib2.Request(url)
auth = 'Basic ' + base64.urlsafe_b64encode("%s:%s" % (USER, PASS))
req.add_header('Authorization', auth)
return urllib2.urlopen(req)
Here's what does NOT work with POST (causes HTTP 400 error):
API_URL = "http://example.com/api/"
def call_api(method, **kwargs):
url = API_URL + method
data=''
if kwargs:
data=urllib.urlencode(kwargs)
req = urllib2.Request(url, data)
auth = 'Basic ' + base64.urlsafe_b64encode("%s:%s" % (USER, PASS))
req.add_header('Authorization', auth)
return urllib2.urlopen(req)
Does anything jump out at anyone as being inherently incorrect in the POST code? I've never done a POST call before but everything I've read seems to suggest that my code is reasonable. Is there some different way I'm supposed to do the add_header thing for the authorization if I'm using POST?