烧瓶 CORS 在请求后触发错误



我有一个用FlaskFlask-Restplus构建的API。我的文件api.py包含以下内容:

from flask import Flask, request, Blueprint, abort, jsonify, url_for
from flask_cors import CORS
from flask_restplus import Resource, Api, fields
[...]
app = Flask(__name__)
CORS(app)
# Create blue print to indicate api base url
blueprint = Blueprint('api', __name__, url_prefix='/cs/api')
api = Api(
blueprint,
title='My API',
version='v1',
description='Some description',
doc='/doc',
contact='my@email.com')
Api.specs_url = specs_url
app.register_blueprint(blueprint)
# All endpoints are defined here and work fine
[...]
@app.after_request
def audit_trail(response):
if request.method != 'OPTIONS':
# Do stuff
[...]
print('Bye')
return True

我的 API 曾经工作得很好,但由于我添加了@app.after_request块,Flask-CORS包为所有 http 请求触发以下错误消息:

if resp.headers.get(ACL_ORIGIN(:

属性错误:"布尔"对象没有属性"标头">

请参阅下面的跟踪:

Bye
[2018-05-23 19:03:00,468] ERROR in app: Exception on /cs/api/doc [GET]
Traceback (most recent call last):
File "/home/alexis/Conda/envs/cs/lib/python3.5/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/home/alexis/Conda/envs/cs/lib/python3.5/site-packages/flask/app.py", line 1643, in full_dispatch_request
response = self.process_response(response)
File "/home/alexis/Conda/envs/cs/lib/python3.5/site-packages/flask/app.py", line 1862, in process_response
response = handler(response)
File "/home/alexis/Conda/envs/cs/lib/python3.5/site-packages/flask_cors/extension.py", line 167, in cors_after_request
if resp.headers.get(ACL_ORIGIN):
AttributeError: 'bool' object has no attribute 'headers'

知道为什么以及如何解决它吗?

问题是因为该方法audit_trail返回True而它应该返回response对象。

相关内容

  • 没有找到相关文章

最新更新