I am trying to access the web API using the POST method. I can access it using the GET method, but the API owners tell me that certain functionality only works with POST. Unfortunately, I cannot get POST to work.
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 is what does NOT work with POST (it causes an 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 something jump on someone because it is incorrect in the POST code? I have never made a POST call before, but everything I read seems to suggest that my code is reasonable. Is there any other way that I have to do the add_header thing for authorization if I use POST?
neomech
source share