'Posting XML Data to an API with Requests & ElementTree

I'm currently trying to post an XML string to an API that i've created from a pandas DataFrame using LXML (mainly because ElementTree doesn't have an xml_declaration=True parameter?). However i seem to be receiving an 400 status code within the response.

xml_data = ET.tostring(root, encoding='UTF-8', xml_declaration=True)

url = api_url + version + endpoint

headers = {
  "Content-Type": "application/xml"
}

params = {
  "access_token": access_token
}

response = r.post(url, headers=headers, params=params, data=xml_data)

I have noticed when printing my XML string (and also running it through an XML validator) there is a line break after my XML Declaration that the validator didn't seem to like:

"<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n"

Could it be this causing the 400 error code? If so, how can i remove the line break from the string given it's a "Bytes" data type instead of "String" after the ET.tostring() function?

Or is there anything else noticeably wrong about this xml declaration?

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source