[英]Returning a different mime-type in Python Bottle
我有一个简单的Python Bottle应用程序,主要返回HTML页面。
但是一个选项需要返回纯文本(MIME类型的“文本/文本”)
@get('/raw/<id>')
def get_raw(id) :
return get_data(id)
如何告诉函数返回此类型?
根据文档 :
更改默认编码
Bottle使用
Content-Type
标头的charset参数来决定如何编码unicode字符串。 此标头默认为text/html; charset=UTF8
text/html; charset=UTF8
,可以使用Response.content_type
属性或直接设置Response.charset属性进行更改。 (Response
对象在“响应对象 ”部分中进行了描述。)from bottle import response @route('/iso') def get_iso(): response.charset = 'ISO-8859-15' return u'This will be sent with ISO-8859-15 encoding.' @route('/latin9') def get_latin(): response.content_type = 'text/html; charset=latin9' return u'ISO-8859-15 is also known as latin9.'
因此,就您而言,您只需要:
response.content_type = 'text/text; charset=UTF8'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.