httplib HTTPConnection request problem | | |
Hi,
I am having a problem with the httplib HTTPConnection object. While I
can easily send requests that don't have any payload (ie. "get"), I
encounter issues if I want to post xml data. If you look at the class
below, when req == 'getFreeBusyInfo' all functions perfectly, but when
req == 'conflictRequest' I run into problems of the nature "400 Bad
Request". As you will notice, I also tried the shorter HTTPConnection
instance method "request", feeding it a dictionary of the header
arguments and the xml payload, but that didn't fly either. With the
headers in this format, I get the error:
AttributeError: 'str' object has no attribute 'iteritems' in line 737
of httplib.py
However, this dictionary format is exactly the same as what I found in
an example in the python documentation.
Class description and example of the xml payload are as follows:
class Connector:
def __init__(self, server):
self.server = 'http://'+server
#self.h1 = httplib.HTTPConnection('my.proxy.org', 3128)
self.h1 = httplib.HTTPConnection('xxx.xxx.xxx.xxx', 80)
def sendInfo(self, req, confDict, xml=None):
print xml
if req == 'getFreeBusyInfo':
self.h1.putrequest('GET',
self.server+"/servlet/webdav.freebusy?username="+confDict['users']+"&server=netline.de&start="+str(confDict['request_start'])+"&end="+str(confDict['request_end']))
self.h1.putheader('Accept-Language', 'de, en-us;q=0.2')
self.h1.putheader('Translate', 'f')
self.h1.putheader('User-Agent', 'SLOX HolidayInfo')
self.h1.putheader('Host', self.server)
self.h1.endheaders()
elif req == 'conflictRequest':
self.h1.putrequest("POST",
self.server+"/servlet/webdav.calendar/conflict.xml")
self.h1.putheader("Accept-Language", "de, en-us;q=0.2")
self.h1.putheader("Translate", "f")
self.h1.putheader("User-Agent", "SLOX HolidayInfo")
self.h1.putheader("Host", confDict['server'])
self.h1.putheader("Content-Length", str(len(xml)))
self.h1.putheader("Authorization", "Basic
"+str(confDict['base64Auth']))
self.h1.endheaders()
self.h1.send(xml)
#headers = {"Accept-Language": "de, en-us;q=0.2", "Translate": "f",
"User-Agent": "SLOX HolidayInfo", "Host": confDict['server'],
"Content-Length": str(len(xml)), "Authorization": "Basic
"+str(confDict['base64Auth'])}
#self.h1.request('POST',
self.server+"/servlet/webdav.calendar/conflict.xml", headers, xml)
def receiveInfo(self):
rec = self.h1.getresponse()
return rec
def closeConnection(self):
print 'Closing connection....'
self.h1.close()
The XML data looks something like this:
<?xml version="1.0" encoding="UTF-8"?><D:multistatus
xmlns:D="DAV:"><D:propertyupdate
xmlns:D="DAV"><D:set><D:prop><S:begins
xmlns:S="SLOX:">1081807200000</S:begins><S:ends
xmlns:S="SLOX:">1081979940000</S:ends><S:clientid
xmlns:S="SLOX:">HolidyInfoID</S:clientid><S:folderid
xmlns:S="SLOX:"></S:folderid><S:title xmlns:S="SLOX:">HolidayInfo
Conflict Request</S:title><S:appointment_request
xmlns:S="SLOX:">yes</S:appointment_request><S:members
xmlns:S="SLOX:"><S:member>xyzxyz</S:member></S:members></D:prop></D:set></D:propertyupdate></D:multistatus>
Incidentally, I set this same thing up using sockets and everything
worked perfectly. However, I need to now use with a proxy so that is
why I moved to the httplib module. I also tried the urllib2 module
but the only request data to be sent is
application/x-www-form-urlencoded and I need to send xml.
If someone can help me out here, I would certainly appreciate it.
Thanks,
Scum | | | | re: httplib HTTPConnection request problem
First make sure that you urlencode the xml like the docs do:
params = urllib.urlencode({'xml': xml})
Make sure the key is something the server is expecting. Then make sure you call request properly:
self.h1.request('POST', self.server+"/servlet/webdav.calendar/conflict.xml",
params, headers)
Notice that params comes before headers.
Not tested but that looks like what your problem might be...
phda
On 31 Mar 2004 10:02:45 -0800 scummer@3squares.com (scummer) wrote:
[color=blue]
> #headers = {"Accept-Language": "de, en-us;q=0.2", "Translate": "f",
> "User-Agent": "SLOX HolidayInfo", "Host": confDict['server'],
> "Content-Length": str(len(xml)), "Authorization": "Basic
> "+str(confDict['base64Auth'])}
> #self.h1.request('POST',
> self.server+"/servlet/webdav.calendar/conflict.xml", headers, xml)[/color] | | | | re: httplib HTTPConnection request problem
scummer wrote:[color=blue]
> Hi,
>
> I am having a problem with the httplib HTTPConnection object. While I
> can easily send requests that don't have any payload (ie. "get"), I
> encounter issues if I want to post xml data. If you look at the class
> below, when req == 'getFreeBusyInfo' all functions perfectly, but when
> req == 'conflictRequest' I run into problems of the nature "400 Bad
> Request". As you will notice, I also tried the shorter HTTPConnection
> instance method "request", feeding it a dictionary of the header
> arguments and the xml payload, but that didn't fly either. With the
> headers in this format, I get the error:[/color]
dunno what's wrong with your code, but i recently used HTTPConnection
to transfer an XML payload to an apache + webware setup. the code below
actually works:
# --- BEGIN ---
from httplib import HTTPConnection
atomMIME = 'application/x.atom+xml'
conn = HTTPConnection('locahost', 80)
print '--------------------------------'
print 'Create a new entry'
print '--------------------------------'
headers = {'Content-Type': atomMIME}
postData = '''
<?xml version="1.0" encoding="iso-8859-1"?>
<entry xmlns="http://purl.org/atom/ns#">
<title>A post</title>
<created>2003-08-12T23:53:03Z</created>
<summary>An automated post</summary>
<content type="text/html" mode="escaped">This is a test</content>
</entry>
'''.strip()
conn.request('POST','/reflex/atom/AtomHandler', postData, headers)
res = conn.getresponse()
assert res.status == 201 # created?
print res.status, res.reason
entryURI = res.msg['location']
print "location:", entryURI
print '*** PASSED ***'
conn.close()
# --- END ---
hope this helps.
--
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<#me> a foaf:Person ; foaf:nick "deelan" ;
foaf:weblog <http://www.deelan.com/> . |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,467 network members.
|