[Tutor] urllib2 and content-type header
Mozaic Web Development
webdev at mozaicsands.com
Sun Apr 29 02:51:51 CEST 2007
Hello -
I'm trying to work with the Basecamp API with Python, and am running
into a bit of an issue. I'm using a Basecamp class that wraps the API
that was written by Jochen Kupperschmidt (if you are reading this,
thanks!) With some help from the basecamp forums, I think I've
identified the issue, in that the urllib2 request that is being
generated is being sent out with a 'Content-type: application/x-www-
form-urlencoded', whereas Basecamp is expecting a header with
'Content-Type: application/xml'. Ok, great - from the docs I see that
this is the default. But from what I can tell, the class compensates
for this and should be overriding the default - but catching the
request in a proxy, it isn't! So any tips would be greatly appreciated..
here are the relevant parts of the class:
import base64, urllib2
import elementtree.ElementTree as ET #(I'm running python 2.4)
class Basecamp(object):
def __init__(self, baseURL, username, password):
self.baseURL = baseURL
if self.baseURL[-1] == '/':
self.baseURL = self.baseURL[:-1]
self.opener = urllib2.build_opener()
self.opener.addheaders = [
('Content-Type', 'application/xml'),
('Accept', 'application/xml'),
('Authorization', 'Basic %s' \
% base64.encodestring('%s:%s' % (username, password)))]
def _request(self, path, data=None):
if isinstance(data, ET._ElementInterface):
data = ET.tostring(data)
req = urllib2.Request(url=self.baseURL + path, data=data)
return self.opener.open(req).read()
So I don't know why the Content-Type that is being set in
self.opener.addheaders isn't sticking.. Thank you, I apologize if
this email is a bit long..
Thanks!
-Eric
More information about the Tutor
mailing list