Flask CORS 在请求后触发错误
Flask CORS triggers error after request
我有一个用 Flask
和 Flask-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):
AttributeError: 'bool' object has no attribute 'headers'
查看下面的跟踪:
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
returned True
而它应该 return response
对象。
我有一个用 Flask
和 Flask-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):
AttributeError: 'bool' object has no attribute 'headers'
查看下面的跟踪:
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
returned True
而它应该 return response
对象。