Unexpected 411 error response using httplib

John Gordon gordon at panix.com
Mon Aug 31 21:44:03 CEST 2009


I'm writing some code that queries a Microsoft Exchange Web Services server.
The server is responding with a 411 Length Required error, which is strange
because I am definitely sending a Content-Length header.

Here's the code:

-----------------------------------------------------
import httplib
import base64

SoapMessage = """\
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
  <soap:Body>
    <FindItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
              xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
              Traversal="Shallow">
      <ItemShape>
        <t:BaseShape>Default</t:BaseShape>
        <t:AdditionalProperties>
          <t:FieldURI FieldURI="calendar:Start" />
          <t:FieldURI FieldURI="calendar:End" />
          <t:FieldURI FieldURI="calendar:LegacyFreeBusyStatus" />
        </t:AdditionalProperties>
      </ItemShape>
      <ParentFolderIds>
        <t:DistinguishedFolderId Id="calendar" />
      </ParentFolderIds>
    </FindItem>
  </soap:Body>
</soap:Envelope>\
"""

host = "some.host.com"

username = "myUsername"
password = "myPassword"
auth = base64.encodestring(username + ":" + password)

conn = httplib.HTTPSConnection(host)
conn.set_debuglevel(5)
conn.putrequest("POST", "/EWS/Exchange.asmx")
conn.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
conn.putheader("Proxy-Authorization", "Basic %s" % auth)
conn.putheader("Content-Length", "%d" % len(SoapMessage))
conn.putheader("User-Agent", "Python post")
conn.endheaders()
conn.send(SoapMessage)

resp = conn.getresponse()

body    = resp.read()
headers = resp.msg
version = resp.version
status  = resp.status
reason  = resp.reason

conn.close()

print "Response: ", status, reason
print "Headers: ", headers
print body
-----------------------------------------------------

As you can see, I am including the call to putheader() for Content-Length,
and the debugging output confirms that the header is present in the outgoing
message.

So why am I getting a 411 Length Required error?

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"




More information about the Python-list mailing list