Just send xml bytes directly:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import requests

xml = """<?xml version='1.0' encoding='utf-8'?>
<a>б</a>"""
headers = {'Content-Type': 'application/xml'} # set what your server accepts
print requests.post('http://httpbin.org/post', data=xml, headers=headers).text

Output

{
  "origin": "x.x.x.x",
  "files": {},
  "form": {},
  "url": "http://httpbin.org/post",
  "args": {},
  "headers": {
    "Content-Length": "48",
    "Accept-Encoding": "identity, deflate, compress, gzip",
    "Connection": "keep-alive",
    "Accept": "*/*",
    "User-Agent": "python-requests/0.13.9 CPython/2.7.3 Linux/3.2.0-30-generic",
    "Host": "httpbin.org",
    "Content-Type": "application/xml"
  },
  "json": null,
  "data": "<?xml version='1.0' encoding='utf-8'?>\n<a>\u0431</a>"
}
Answer from jfs on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › posting xml data to an api using python requests & elementtree/lxml
r/learnpython on Reddit: Posting XML Data to an API using Python Requests & ElementTree/lxml
April 19, 2022 -

Hello

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

EDIT:
The response text error i'm actually getting is:

{"error":{"message":"(#10801) Either \"file\" or \"url\" must be specified.","type":"OAuthException","code":10801}}

So is it that i'm actually passing the data into requests incorrectly?

Discussions

python - POST XML file with requests - Stack Overflow
I'm getting: You have an error in your XML syntax... when I run this python script I just wrote (I'm a newbie) import requests xml = """xxx.xml""" headers = {'Content-Type':'text/x... More on stackoverflow.com
🌐 stackoverflow.com
Post Request grabs xml in postman, but when I use the python requests code generated it doesn't run
Also, the post request only works in the chrome extension and NOT in the standalone app. I get 401’s when using the standalone app. Anybody have a similar issue or know how to fix this? More on community.postman.com
🌐 community.postman.com
1
0
July 23, 2019
XML file upload in requests library
I am attempting to send a POST request which passes an XML file to an endpoint. I don’t understand how it should be written in the Robot Framework Requests Library. The code below is written in Python and works great. For some reason I’m having trouble converting it to the Robot Framework ... More on forum.robotframework.org
🌐 forum.robotframework.org
2
0
March 14, 2023
Posting XML data in the body of a post request leads to an error
What is the exact error message you are getting? More on reddit.com
🌐 r/learnpython
7
1
December 5, 2023
🌐
ReqBin
reqbin.com › req › python › z1miyucm › get-xml-example
Python | How do I get an XML from the server?
January 15, 2023 - To post XML to the server, you need to make a POST request, include the XML in the request body and specify the correct MIME type. The MIME type for XML is "application/xml". The Accept: application/xml request header tells the server that the ...
🌐
On Test Automation
ontestautomation.com › writing-tests-for-restful-apis-in-python-using-requests-part-3-working-with-xml
Writing tests for RESTful APIs in Python using requests – part 3: working with XML | On Test Automation
January 10, 2020 - Note that we explicitly set the ... the XML request body is done by assigning the return value of our method returning the XML as a string to the data parameter of the requests post() method....
🌐
ReqBin
reqbin.com › code › python › ighnykth › python-requests-post-example
How do I send a POST request using Python Requests Library?
Requests Library is a very popular library for sending HTTP requests in Python. The Requests Library simplifies the process of working with HTTP requests in Python. The Requests library is very flexible and can send any type of HTTP request using POST, GET and DELETE methods, upload files, post JSON and XML, and submit HTML forms.
Find elsewhere
🌐
Robot Framework
forum.robotframework.org › libraries › requestslibrary
XML file upload in requests library - RequestsLibrary - Robot Framework
March 14, 2023 - I am attempting to send a POST request which passes an XML file to an endpoint. I don’t understand how it should be written in the Robot Framework Requests Library. The code below is written in Python and works great. F…
🌐
Codegive
codegive.com › blog › requests_post_xml.php
requests post xml (2026): Master API Interactions & Streamline Data Exchange – Why Your Python Apps Need This Now
This is critical for APIs that explicitly expect XML for operations like creating resources, updating records, or executing specific business logic. The core components involved are: The requests.post() method: The Python function to initiate the POST request.
🌐
Reddit
reddit.com › r/learnpython › posting xml data in the body of a post request leads to an error
r/learnpython on Reddit: Posting XML data in the body of a post request leads to an error
December 5, 2023 -

Hey all,
I have been trying to debug this for a few hours now.
I am POSTing data to an endpoint where the body must be an XML.
I have taken my Postman request and saved it as a XML and I am passing it into a requests.post() in python.
Every time I get an error message. Looking at App Insights on the API side I see that the last 2 symbols from the XML are missing the XML end as:
</dataElemen
where it should end up like:
</dataElement>
In python I do not modify the XML file. The XML file is a copy of the XML from Postman that works as expected.
The only explanation that I have is that Content-Length gets overwritten when the post request is executed because I tried adding + 2 to the length to account for the 2 last missing symbols.
The xml is utf-8 and there is nothing unusual about it.
Do you have any suggestions what I can try to pass the correct xml?

Link to code - https://imgur.com/a/lGgk6J5 (there is a bit of sensitive data that I have redacted, that is why I am pasting an image)

🌐
Jeroen Baert's Blog
forceflow.be › home › http xml post using python
HTTP XML Post using Python - Jeroen Baert's Blog
December 16, 2022 - This should work on all Python versions (both 2.x and 3) and doesn’t use any nonstandard libraries. Of course, you don’t have to read XML from a file, you can easily modify the method to work with an XML string. In this example, I’m posting to an HTTP host. You could also easily modify this to work over HTTPS by using httplib.HTTPS as a webservice. import sys, httplib HOST = www.example.com API_URL = /your/api/url def do_request(xml_location): """HTTP XML Post request, by www.forceflow.be""" request = open(xml_location,"r").read() webservice = httplib.HTTP(HOST) webservice.putrequest("PO
🌐
DEV Community
dev.to › nicfoxds › how-to-get-data-from-an-api-process-xml-in-python-4c1
How to Get Data from an API & Process XML in Python - DEV Community
July 5, 2020 - Share Post via... Report Abuse ... Thanks to the requests library it is quick and easy to get data from an API, even for beginners, and the ElementTree library makes it easy to process the XML that an API provides. This tutorial, aimed at beginners, will show you an end-to-end example using python to get information about property listings from the Zoopla API.
🌐
Automated Ramblings
sdbrett.com › BrettsITBlog › 2017 › 03 › python-parsing-api-xml-response-data
Python: Parsing API XML Response Data – Automated Ramblings
March 21, 2017 - Parsing the response in XML slowed things down a little. Which is the focus of the post. How to parse the XML response with Python. After some time I put the pieces together can work with this product through the API. You will need two modules. Requests and ElementTree.
🌐
GitHub
github.com › asyrjasalo › RESTinstance › issues › 27
Trouble sending XML in body when using POST · Issue #27 · asyrjasalo/RESTinstance
August 6, 2018 - *** Settings *** Library REST *** Variables *** ${SERVER_URL} https://www.w3schools.com/xml/tempconvert.asmx *** Test Cases *** Send XML Body test Send SOAP via POST <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><FahrenheitToCelsius xmlns="https://www.w3schools.com/xml/"><Fahrenheit>100</Fahrenheit></FahrenheitToCelsius></soap:Body></soap:Envelope> *** Keywords *** Send SOAP via POST [Arguments] ${body} SET HEADERS {"SOAPAct
Author   asyrjasalo
🌐
Python Forum
python-forum.io › thread-29639.html
Python Requests package: Handling xml response
I'm learning Python programming and I just completed classes about lists, tuples, functions, and methods I like very much the requests package and its comfortable way to handle JSON responses. Unfortu