httplib HTTPConnection request problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • scummer

    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 == 'getFreeBusyInf o' all functions perfectly, but when
    req == 'conflictReques t' 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.HTTPCon nection('my.pro xy.org', 3128)
    self.h1 = httplib.HTTPCon nection('xxx.xx x.xxx.xxx', 80)

    def sendInfo(self, req, confDict, xml=None):
    print xml
    if req == 'getFreeBusyInf o':
    self.h1.putrequ est('GET',
    self.server+"/servlet/webdav.freebusy ?username="+con fDict['users']+"&server=netli ne.de&start="+s tr(confDict['request_start'])+"&end="+str(c onfDict['request_end']))
    self.h1.puthead er('Accept-Language', 'de, en-us;q=0.2')
    self.h1.puthead er('Translate', 'f')
    self.h1.puthead er('User-Agent', 'SLOX HolidayInfo')
    self.h1.puthead er('Host', self.server)
    self.h1.endhead ers()
    elif req == 'conflictReques t':
    self.h1.putrequ est("POST",
    self.server+"/servlet/webdav.calendar/conflict.xml")
    self.h1.puthead er("Accept-Language", "de, en-us;q=0.2")
    self.h1.puthead er("Translate" , "f")
    self.h1.puthead er("User-Agent", "SLOX HolidayInfo")
    self.h1.puthead er("Host", confDict['server'])
    self.h1.puthead er("Content-Length", str(len(xml)))
    self.h1.puthead er("Authorizati on", "Basic
    "+str(confD ict['base64Auth']))
    self.h1.endhead ers()
    self.h1.send(xm l)
    #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(confD ict['base64Auth'])}
    #self.h1.reques t('POST',
    self.server+"/servlet/webdav.calendar/conflict.xml", headers, xml)

    def receiveInfo(sel f):
    rec = self.h1.getresp onse()
    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:multista tus
    xmlns:D="DAV:"> <D:propertyupda te
    xmlns:D="DAV">< D:set><D:prop>< S:begins
    xmlns:S="SLOX:" >108180720000 0</S:begins><S:end s
    xmlns:S="SLOX:" >108197994000 0</S:ends><S:clien tid
    xmlns:S="SLOX:" >HolidyInfoID </S:clientid><S:f olderid
    xmlns:S="SLOX:" ></S:folderid><S:t itle xmlns:S="SLOX:" >HolidayInfo
    Conflict Request</S:title><S:appo intment_request
    xmlns:S="SLOX:" >yes</S:appointment_r equest><S:membe rs
    xmlns:S="SLOX:" ><S:member>xyzx yz</S:member></S:members></D:prop></D:set></D:propertyupdat e></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
  • pythonhda

    #2
    Re: httplib HTTPConnection request problem

    First make sure that you urlencode the xml like the docs do:

    params = urllib.urlencod e({'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@3square s.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(confD ict['base64Auth'])}
    > #self.h1.reques t('POST',
    > self.server+"/servlet/webdav.calendar/conflict.xml", headers, xml)[/color]

    Comment

    • deelan

      #3
      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 == 'getFreeBusyInf o' all functions perfectly, but when
      > req == 'conflictReques t' 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('P OST','/reflex/atom/AtomHandler', postData, headers)
      res = conn.getrespons e()
      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/> .

      Comment

      Working...