Answer the question
In order to leave comments, you need to log in
How to create xml in memory, attach it to json and send?
Good day.
Can you please tell me how to create xml in buffer, attach it to json and send?
def send_json(**kwargs):
send_to = 'http://bla-bla.ru/external/'
msg = {}
msg.update(kwargs)
context = MyObject.objects.get(pk=kwargs.get('id'))
xml = render_to_string('xml_template_min.xml',
{'object': context})
f = io.TextIOBase(xml, encoding='text/xml;name="myobject.xml"')
r = requests.post(send_to, files=f, data=msg)
Answer the question
In order to leave comments, you need to log in
>>> from xml.etree import ElementTree as ET
>>> root = ET.Element("root")
>>> ET.SubElement(root, 'b')
>>> ET.SubElement(root, 'a')
>>> ET.SubElement(root, 'c')
>>> ET.tostring(root)
'<root><b /><a /><c /></root>'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question