flask端点的return语句的默认返回类型是什么?

如果我们不使用render_template或标记或make_response或jsonify;并且仅使用返回值“ somevalue”,那么响应的类型是什么?

a314672413 回答:flask端点的return语句的默认返回类型是什么?

我认为这是HTML。 通过“ Hello World”观看他们的reference

,

这将返回内容类型:text / html;

,

不,您不能返回一些任意值。返回类型必须是字符串,元组,Response实例或可调用的WSGI,否则您将得到TypeError: The view function did not return a valid response。对于字符串类型,将使用该数据和默认参数(例如Content-Type: text/html; charset=utf-8)创建响应对象。元组值可以用于返回类型(response,status,headers)(response,headers)的特定顺序是:在元组中必须至少有一项。对于可调用的WSGI,您可以根据需要设置标题。

def simple_resp(environ,start_response):
    status = '200 OK'
    response_headers = [('Content-Length','13')]
    start_response(status,response_headers)
    return ['Hello World!\n']

@application.route('/minion')
def minion():
    return simple_resp

我认为您已经熟悉Response类型。

本文链接:https://www.f2er.com/3144042.html

大家都在问