简体   繁体   English

Flask-restful是否可以返回Response对象?

[英]Can flask-restful return Response object?

When implementing REST API with Flask-Restful, I wanted to return a Flask's Response object, because it is flexible and easy to use. 使用Flask-Restful实现REST API时,我想返回Flask的Response对象,因为它灵活且易于使用。 For example: 例如:

return Response(
            response=jsonify({
                "data": {
                    "import_id": import_id
                }
            }),
            status=201,
            mimetype="application/json"
        )

But looks like flask-restless doesn't support such a thing: 但看起来烧瓶躁动不支持这种事情:

2019-08-05 08:52:30,077 werkzeug     INFO     172.21.0.1 - - [05/Aug/2019 08:52:30] "POST /imports HTTP/1.1" 500 -
2019-08-05 08:52:30,078 werkzeug     ERROR    Error on request:
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/werkzeug/serving.py", line 303, in run_wsgi
    execute(self.server.app)
  File "/usr/local/lib/python3.7/site-packages/werkzeug/serving.py", line 293, in execute
    for data in application_iter:
  File "/usr/local/lib/python3.7/site-packages/werkzeug/wsgi.py", line 507, in __next__
    return self._next()
  File "/usr/local/lib/python3.7/site-packages/werkzeug/wrappers/base_response.py", line 45, in _iter_encoded
    for item in iterable:
TypeError: 'Response' object is not iterable

If there are any workaround to use Response? 是否有使用应急响应的解决方法? If not, how can I return a mimetype with flask-restfull ? 如果没有,如何返回带有flask-restfull的模仿类型

jsonify() function returns a flask.Response() object, while json.dumps(obj) Serializes obj to a JSON-formatted string. jsonify()函数返回一个flask.Response()对象,而json.dumps(obj)序列化obj的JSON格式的字符串。


....
from Flask import json, Response

....
   return Response(
        response=json.dumps({
            "data": {
                "import_id": import_id
            }
        }),
        status=201,
        mimetype="application/json"
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM