I am using Flask and I return an XML file from a get request. How do I set the content type to xml ?

e.g.

@app.route('/ajax_ddl')
def ajax_ddl():
    xml = 'foo'
    header("Content-type: text/xml")
    return xml

推荐答案

Try like this:

from flask import Response
@app.route('/ajax_ddl')
def ajax_ddl():
    xml = 'foo'
    return Response(xml, mimetype='text/xml')

The actual Content-Type is based on the mimetype parameter and the charset (defaults to UTF-8).

Response (and request) objects are documented here: http://werkzeug.pocoo.org/docs/wrappers/

Python相关问答推荐

在Google Colab中设置Llama-2出现问题-加载判断点碎片时Cell-run失败

如何制作10,000年及以后的日期时间对象?

我们可以为Flask模型中的id字段主键设置默认uuid吗

使用密钥字典重新配置嵌套字典密钥名

将输入聚合到统一词典中

调用decorator返回原始函数的输出

如何在Python中获取`Genericums`超级类型?

与命令行相比,相同的Python代码在Companyter Notebook中运行速度慢20倍

幂集,其中每个元素可以是正或负""""

替换现有列名中的字符,而不创建新列

删除特定列后的所有列

如何将相同组的值添加到嵌套的Pandas Maprame的倒数第二个索引级别

Python日志(log)库如何有效地获取lineno和funcName?

Pandas:将值从一列移动到适当的列

是否将Pandas 数据帧标题/标题以纯文本格式转换为字符串输出?

我怎样才能让深度测试在OpenGL中使用Python和PyGame呢?

合并Pandas中的数据帧,但处理不存在的列

如何将参数名作为参数传入到函数中?

设计添加和搜索词的数据 struct :Leetcode211

捕获脚本和退出代码的多行输出