Reputation: 754
I have a XML-file that I need to send to some URL. I do that this way:
data = { 'file' : open('test.xml', 'rb') }
req = requests.post(URL, files=data)
This works, but problem is that first I need to generate XML, then I need to do this:
et = etree.ElementTree(root)
et.write('test.xml', encoding='utf8')
and then after that I do this:
data = { 'file' : open('test.xml', 'rb') }
req = requests.post(URL, files=data)
But I don't like this, I have XML-file, then I write it to disk, just to read it again from disk.
Is there a way to write that XML directly to file object (equivalent of open('test.xml', 'rb')
) without writing it to file first?
Upvotes: 0
Views: 315
Reputation: 82765
Try using tostring
Ex:
et = etree.ElementTree(root)
req = requests.post(URL, data=etree.tostring(et.getroot()))
Upvotes: 1