gpt4 book ai didi

python - Flask CORS 在请求后触发错误

转载 作者:行者123 更新时间:2023-12-01 09:25:08 25 4
gpt4 key购买 nike

我有一个使用 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 block ,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 返回 True,而它应该返回 response 对象。

关于python - Flask CORS 在请求后触发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50486824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com