How to create XML endpoint in Flask?
All jsonify
is doing is dumping the arguments passed to it with json
and setting the content-type of the response to application/json
. You would do the exact same thing for XML: dump the data (Python has the built-in etree library or there's the more powerful lxml) and set the content-type to application/xml
.
There are many ways you could represent data using XML, so that's up to you, but the basic outline is:
import xml.etree.ElementTree as ETroot = ET.Element('root') # name the root whatever you want# add your data to the root node in the format you wantreturn app.response_class(ET.tostring(root), mimetype='application/xml')