requests does not handle parsing XML responses, no. XML responses are much more complex in nature than JSON responses, how you'd serialize XML data into Python structures is not nearly as straightforward.

Python comes with built-in XML parsers. I recommend you use the ElementTree API:

import requests
from xml.etree import ElementTree

response = requests.get(url)

tree = ElementTree.fromstring(response.content)

or, if the response is particularly large, use an incremental approach:

response = requests.get(url, stream=True)

# if the server sent a Gzip or Deflate compressed response, decompress
# as we read the raw stream:
response.raw.decode_content = True

events = ElementTree.iterparse(response.raw)

for event, elem in events:
    # do something with `elem`

The external lxml project builds on the same API to give you more features and power still.

Answer from Martijn Pieters on Stack Overflow
🌐
ReqBin
reqbin.com › req › python › z1miyucm › get-xml-example
Python | How do I get an XML from the server?
January 15, 2023 - The Content-Type: application/xml response header informs the client that the server returned XML. In this Python GET XML Example, we send a GET request to the ReqBin echo URL with Accept: application/xml request header.
Discussions

How do I send an xml request and receive an xml response in Python? - Stack Overflow
You could use your own URL and XML or adjust not to send XML request and just parse the response against the provided XML. I would also suggest looking into eBays example for their x.Commerce XML API accessed from Python. More on stackoverflow.com
🌐 stackoverflow.com
python - How can I send an xml body using requests library? - Stack Overflow
Can Requests be used to send an XML body generated with xml.etree.ElementTree? 2017-10-27T18:05:32.347Z+00:00 ... @StevenVascellaro it doesn't matter what generates xml as long as it is a bytestring 2017-10-27T18:11:02.153Z+00:00 ... Save this answer. Show activity on this post. Pass in the straight XML ... More on stackoverflow.com
🌐 stackoverflow.com
Posting XML Data to an API using Python Requests & ElementTree/lxml
I don't think your problem is \n. The double slashes before quotes on the other hand... that definitely makes it invalid. I don't know who's adding them though. Could it be something you copied from the output incorrectly? More on reddit.com
🌐 r/learnpython
1
9
April 19, 2022
Simple XML Parsing in Python
I’m programming in python and I’m trying to read the status of my Yamaha network stereo. I get the following xml response On All good so far. Now, I’m trying to search for Power to get the latest status. It is there in XML but I can’t locate the Power Status value when I use a XML parser ... More on community.openhab.org
🌐 community.openhab.org
1
0
January 11, 2018
🌐
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 - Parsing and working with XML response bodies Now that we have covered creating XML request bodies, let’s see what we can do with XML responses. By far the most powerful way to create specific assertions is to convert the XML response body into an ElementTree and then asserting on its properties. As an example, we’re going to perform an HTTP GET call to
🌐
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 - Tip: if you copy-paste the XML into a tool like Sublime Text, you can collapse/expand the tags to make it easier to see the hierarchy of attributes. Now let's start coding. We first need to import the following libraries (install them first if you need to): import requests # for using API import xml.etree.ElementTree as ET # for parsing XML import numpy as np # for using pandas import pandas as pd # for using dataframes
🌐
*ARGS.TECH
args.tech › blog › 19 › working-with-xml-in-python-requests-library
Working with XML in Python Requests library | BLOG | *ARGS.TECH
December 5, 2025 - (env) user@localhost:~/my_project$ python3 main.py breakfast_menu food name price description calories food name price description calories food name price description calories food name price description calories food name price description calories · Now we are write code for getting values from inner elements. Open main.py file and replace previously code with this: import requests import xml.etree.ElementTree as ET request = requests.get('https://www.w3schools.com/xml/simple.xml') root = ET.fromstring(request.content) for item in root.iterfind('food'): print(item.findtext('name')) print(item.findtext('price')) print(item.findtext('description')) print(item.findtext('calories'))
🌐
Sling Academy
slingacademy.com › article › python-requests-module-handle-xml-response
Python ‘requests’ module: Handle XML response - Sling Academy
This tutorial demonstrates parsing XML responses using Python ‘requests’ and other related libraries. To begin, install the ‘requests’ module if it’s not already installed: ... import requests url = 'http://example.com/data.xml' response = requests.get(url) print(response.content)
🌐
DEV Community
dev.to › xinitd › working-with-xml-in-python-requests-library-20ba
Working with XML in Python Requests library - DEV Community
February 14, 2025 - (env) user@localhost:~/my_project$ python3 main.py breakfast_menu food name price description calories food name price description calories food name price description calories food name price description calories food name price description calories · Now we are write code for getting values from inner elements. Open main.py file and replace previously code with this: import requests import xml.etree.ElementTree as ET request = requests.get('https://www.w3schools.com/xml/simple.xml') root = ET.fromstring(request.content) for item in root.iterfind('food'): print(item.findtext('name')) print(item.findtext('price')) print(item.findtext('description')) print(item.findtext('calories'))
Find elsewhere
🌐
GitHub
gist.github.com › StanGirard › 204eb424781b187f46771064e5819666
Parsing an API XML Response Data - Python · GitHub
Parsing an API XML Response Data - Python · Raw · Parsing an API XML Response Data · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
PyPI
pypi.org › project › requests-xml
requests-xml
March 24, 2018 - JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
Bright Data
brightdata.com › blog › how-tos › parsing-xml-in-python
How to Parse XML in Python? Multiple Methods Covered
September 16, 2025 - untangle is not available by default ... XML elements: import untangle import requests url = "https://brightdata.com/post-sitemap.xml" response = requests.get(url) if response.status_code == 200: obj = untangle.parse(response.text) ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › xml-parsing-python
XML parsing in Python - GeeksforGeeks
June 28, 2022 - For more insight on how requests module works, follow this article: GET and POST requests using Python · Parsing XML We have created parseXML() function to parse XML file. We know that XML is an inherently hierarchical data format, and the most natural way to represent it is with a tree. Look at the image below for example: Here, we are using xml.etree.ElementTree (call it ET, in short) module.
🌐
Ex Libris
developers.exlibrisgroup.com › home › how to parse the xml object returned from analytics api with python
How to parse the xml object returned from analytics API with Python - Ex Libris Developer Network
November 18, 2019 - Following is a code snippet for how to iterate through the results: import xml.etree.cElementTree as et report = requests.get(URL) # print("\n\nReport: \n" + json.dumps(report) + "\n") # print ("\n\n\n\n" + str(report.content) + "\n\n\n\n") report_string = report.content tree = et.ElementT...
🌐
Automated Ramblings
sdbrett.com › BrettsITBlog › 2017 › 03 › python-parsing-api-xml-response-data
Python: Parsing API XML Response Data – Automated Ramblings
March 21, 2017 - Requests: http://docs.python-requests.org/en/master/ ElementTree: https://docs.python.org/3.6/library/xml.etree.elementtree.html#module-xml.etree.ElementTree · If you’;re unfamilar with XML terminology, I’;d recommend having a quick look here. To practice with this, we need XML formatted text.
🌐
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?

🌐
Python-requests
xml.python-requests.org
Domain Suspension
As a Registrant of a domain name, a new domain registration or changes to the first name, last name or email address of an existing domain name, requires verification of the information within 15 days. Since you failed to verify the Registrant information, this domain name and any other domain ...
🌐
GitHub
github.com › erinxocon › requests-xml
GitHub - erinxocon/requests-xml: Requests-XML: XML Parsing for Humans · GitHub
Using the great xmljson package, we convert the whole XML document into a JSON representation. There are six different conversion convetions available. See the about for what they are. The default is badgerfish. If you wish to use a different conversion convention, pass in a string with the name of the convetion to the .json() method. ... >>> from requests_xml import XML >>> doc = """ <employees> <person> <name value="Alice"/> </person> <person> <name value="Bob"/> </person> </employees> """ >>> xml = XML(xml=doc) >>> xml.json() { "employees": [{ "person": { "name": { "@value": "Alice" } } }, { "person": { "name": { "@value": "Bob" } } }] }
Starred by 186 users
Forked by 6 users
Languages   Python 99.3% | Makefile 0.7%
🌐
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. Unfortunately, I did not understand if I can also proces...
🌐
openHAB Community
community.openhab.org › setup, configuration and use › scripts & rules
Simple XML Parsing in Python - Scripts & Rules - openHAB Community
January 11, 2018 - I’m programming in python and I’m trying to read the status of my Yamaha network stereo. I get the following xml response On All good so far. Now, I’m trying to search for Power to get the latest status. It is there in XML but I can’t locate the Power Status value when I use a XML parser such as Elementtree. import requests from xml.etree import E...