虽然mimetype = text / plain

时间:2017-12-05 12:50:05

标签: flask flask-restful

我有一个API,我将application/json数据发布到我希望从中返回text/plain的数据。我收到以下错误:

TypeError: Object of type 'Response' is not JSON serializable

即使我显式设置text/plain作为mimetype:

return Response(ret_str, mimetype='text/plain')

我的帖子如下:

headers = {"Content-Type":"application/json", "Accept":"text/plain"}
r = requests.post(url, data = json.dumps(data), headers = headers)

我做错了什么?

编辑:完全回溯

Traceback (most recent call last):
  File "/root/miniconda3/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/root/miniconda3/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/root/miniconda3/lib/python3.6/site-packages/flask_restful/__init__.py", line 484, in wrapper
    return self.make_response(data, code, headers=headers)
  File "/root/miniconda3/lib/python3.6/site-packages/flask_restful/__init__.py", line 513, in make_response
    resp = self.representations[mediatype](data, *args, **kwargs)
  File "/root/miniconda3/lib/python3.6/site-packages/flask_restful/representations/json.py", line 21, in output_json
    dumped = dumps(data, **settings) + "\n"
  File "/root/miniconda3/lib/python3.6/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/root/miniconda3/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/root/miniconda3/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/root/miniconda3/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'Response' is not JSON serializable

0 个答案:

没有答案