Flask @使用_kwargs更改响应内容
我注意到Flask @使用_kwargs更改响应内容,flask,flask-apispec,Flask,Flask Apispec,我注意到flask apispec中的@use_kwargs更改了响应内容类型。在下面的“hello world”示例中,使用@use_kwargs将响应内容类型从text/html更改为application/json。我觉得有点奇怪,因为没有提到它,我也不希望注入args也会改变响应类型: 从烧瓶导入烧瓶 从烧瓶导入apispec使用kwargs 从棉花糖进口领域 app=烧瓶(名称) @应用程序路径(“/”) @使用_-kwargs({'email':fields.Str()},locat
flask apispec
中的@use_kwargs
更改了响应内容类型。在下面的“hello world”示例中,使用@use_kwargs
将响应内容类型从text/html
更改为application/json
。我觉得有点奇怪,因为没有提到它,我也不希望注入args也会改变响应类型:
从烧瓶导入烧瓶
从烧瓶导入apispec使用kwargs
从棉花糖进口领域
app=烧瓶(名称)
@应用程序路径(“/”)
@使用_-kwargs({'email':fields.Str()},location=“query”)
def hello_world(**kwargs):
返回“你好,世界!
curl-vhttp://127.0.0.1:5000/\?电子邮件\=abc
显示响应
> GET /?email=abc HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.64.1
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: application/json
< Content-Length: 16
< Server: Werkzeug/1.0.1 Python/3.8.2
< Date: Tue, 12 Jan 2021 06:09:25 GMT
<
"Hello, World!"
改变回应的理由是什么?即使使用@use\u kwargs
,如何设置“text/html”的响应内容类型?谢谢大家!
更新:
只需在@Diego Miguel给出的答案上添加更多细节:
改变内容的效果是由中的逻辑引起的
marshal_result
调用,使对象具有“application/json”mimetype,并引入了上面“hello world”示例的额外引用级别。我不完全清楚为什么使用@use_kwargs
会更改内容类型。通过查看,它似乎返回了一个dict
,根据这一点判断(这被activate
调用)。因此,我最好的猜测是,当执行app.routejsonify时,烧瓶是dict
,因为。此时,内容类型
更改为application/json
。但是,hello\u world
在use\u kwargs
之后执行,最后返回一个字符串,即“hello world!”
无论如何,我认为flask apispec
实际上并不打算这样做
您可以更改响应的内容类型
(以及任何其他字段),使用make\u response
创建一个Flask.response
对象,然后将其内容类型
设置为“text/html”
(但是,在将字符串传递给make\u response
时,默认情况下会设置此项,因此无需设置):
curl-v的输出http://127.0.0.1:5000/\?电子邮件\=abc
:
> GET /?email=abc HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.74.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/html
< Content-Length: 13
< Server: Werkzeug/1.0.1 Python/3.9.1
< Date: Tue, 12 Jan 2021 19:08:05 GMT
<
Hello World!
>GET/?email=abc HTTP/1.1
>主持人:127.0.0.1:5000
>用户代理:curl/7.74.0
>接受:*/*
>
*将捆绑包标记为不支持多用途
*HTTP 1.0,假设在正文之后关闭
def __call__(self, *args, **kwargs):
response = self.call_view(*args, **kwargs)
if isinstance(response, werkzeug.Response):
return response
rv, status_code, headers = unpack(response)
mv = self.marshal_result(rv, status_code)
response = packed(mv, status_code, headers)
return flask.current_app.make_response(response)
from flask import Flask, make_response
from flask_apispec import use_kwargs
from marshmallow import fields
app = Flask(__name__)
@app.route('/')
@use_kwargs({'email': fields.Str()}, location="query")
def hello_world(**kwargs):
response = make_response("Hello World!")
response.content_type = 'text/html' # can be omitted
return response
> GET /?email=abc HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.74.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/html
< Content-Length: 13
< Server: Werkzeug/1.0.1 Python/3.9.1
< Date: Tue, 12 Jan 2021 19:08:05 GMT
<
Hello World!