Python Flask,如何设置内容类型 [英] Python Flask, how to set content type

查看:151
本文介绍了Python Flask,如何设置内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask,并从get请求中返回一个XML文件。如何设置内容类型?



例如

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


解决方案

b $ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$ '
return(xml,mimetype ='text / xml')

实际内容类型基于mimetype参数和字符集(默认为UTF-8)。

响应(和请求)对象记录在这里: http://werkzeug.pocoo.org/docs/wrappers/


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

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 Flask,如何设置内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆