Unexpected 411 error response using httplib

J

John Gordon

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?
 
J

John Gordon

In said:
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?

To follow up my own post, this was happening because of a trailing
newline in auth, put there by base64.encodestring().

The newline made the server stop processing any subsequent headers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top