Sending XML payload in Python with base64 Authorization headers
If you try to use Python’s httplib to send a POST request containing an XML payload you might run into this problem as I have, where it seems like the request is always rendered invalid due to a leading new line (\r\n) in front of the body of the request.
I’ve just spent a day solving this problem, with very little help from Google, so I thought I’d add to the Google knowledge pool in case it helps a poor soul save a day’s worth of productivity.
The problem seems to manifest when your headers also include an Authentication header containing a base64 encoded user name and password. It seems like python’s base64.encodestring method adds a courtesy new line after the encoding. Why it does that is beyond me, but that extra new line causes the body to be pushed down. With an XML payload it can cause an error if the receiving end does not trim the body and tries to parse the XML as is – the declaration will then not be the first item in the packet – it will be the new line.
The solution: strip() your base64 encoded string! Simple, but took me a day to find out.
By the way, HTTPConnections can have their debug level set to a higher value in order to dump out all the relating connection debug info. I would probably have spent another day have I not been able to see that output.
July 26th, 2008 at 12:58 pm
Hi.
Thanks for “adding” this to google
It helped me just right now. Was wondering where that newline came from.
/tbc