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/"
    <FindItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
          <t:FieldURI FieldURI="calendar:Start" />
          <t:FieldURI FieldURI="calendar:End" />
          <t:FieldURI FieldURI="calendar:LegacyFreeBusyStatus" />
        <t:DistinguishedFolderId Id="calendar" />

host = "some.host.com"

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

conn = httplib.HTTPSConnection(host)
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")

resp = conn.getresponse()

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


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

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