skip to Main Content

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.

2

Answers


  1. Check the locale at your Debian system.

    Verify you have installed and setup the right locale.

    For the current settings:

    locale
    

    Configured:

    grep -v "^#" /etc/locale.gen
    

    If a relevant locale is missing, edit the file /etc/locale.gen and activate the required entries by deleting the # mark in its line.

    Even if configured right, please issue

    sudo locale-gen
    

    to be sure they really have been generated.

    Login or Signup to reply.
  2. Consider parsing the payload XML template with a DOM library such as Python’s built-in xml.etree and use library to update element text. Avoid treating the XML like a text file with str conversion and formatting on entire content. For string formatting of text, use the current recommended standard of F-strings:

    import xml.etree.ElementTree as et
    ...
    
    # handle default namespace
    et.register_namespace('', "urn:Tool:apis:eBLBaseComponents")
    nmsp = {"doc": "urn:Tool:apis:eBLBaseComponents"}
    
    # parse XML template and replace element text
    msg = et.parse("XMLAPIPayload.xml")
    
    msg.find(".//doc:RequesterCredentials/doc:ToolAuthToken", namespaces = nmsp).text = sessiontoken
    msg.find(".//doc:MemberMessage/doc:Body", namespaces = nmsp).text = (
        f"random text some information {accountName} another information {key}"
    )
    msg.find(".//doc:MemberMessage/doc:RecipientID", namespaces = nmsp).text = userId
    
    # export to byte string
    payload = et.tostring(msg.getroot(), encoding="utf-8")
    
    # prepare XML for API call
    url = "https://myurl.com/api/method"
    
    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
    print(payload)
    response = requests.request("POST", url, headers=headers, data=payload)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search