Search code examples
pythonxmlencodingpython-requestsdebian

Python requests POST method parses XML always as ANSI


I have a script which sends a XML within a POST method using python requests module to an API. This code works from my test machine (windows) when I deploy the script to my PROD server (Debian) I always receive a parsing error. If I change the encoding of the file to ANSI it will work on the Debian server.

It seems to be some kind of encoding issue. However from my point of view I set up everything correctly in order to send the file as utf-8.

This is the relevant code snippet:

# prepare XML for API call
    url = "https://myurl.com/api/method"
    # use XML template and replace some wildcards
    payloadTemplate = open("message.xml", encoding="utf-8")
    payload = payloadTemplate.read()
    payload = str(payload % (sessiontoken, accountName, key, userId))
    headers = {
        'X-XYZ-API-COMPATIBILITY-LEVEL': '1234',
        'X-XYZ-API-CALL-NAME': 'methodname',
        'X-XYZ-API-SITEID': '11',
        'Content-Type': 'application/xml; charset=utf-8'
    }
    # send request
    response = requests.request("POST", url, headers=headers, data=payload)

The XML looks like this:

<?xml version="1.0" encoding="utf-8"?>
<APIMethod xmlns="urn:Tool:apis:eBLBaseComponents">
<RequesterCredentials>
<ToolAuthToken>%s</ToolAuthToken>
</RequesterCredentials>
<ItemID>12345678</ItemID>
<MemberMessage>
<Subject>Thanks</Subject>
<Body>random text some information %s another information %s</Body>
<QuestionType>General</QuestionType>
<RecipientID>%s</RecipientID>
</MemberMessage>
</APIMethod>

Using Postman or the script on Windows is working fine. On Debian it's not working anymore. If I save the XML file in ANSI encoding it will work on Debian as well. I'm using this as workaround, unfortuantely I would like to have the same solutions for Windows and Unix (which is in my case TEST and PROD). Also I would like to include proper German letters.

Glad for any help

I added the charset=utf-8 to the header. It changes nothing un Debian side. In windows its still working. When I'm printing the payload as string, everything is fine. There seems to be no encoding issue.


Solution

  • After a fresh install of Debian the issue no longer exists. Python version remained the same. Therefore, I assume (like I did in the beginning) that this was some kind of Debian caused issue.