可以在谷歌云功能中使用flask_cors
吗?
app = Flask(__name__)
cors = CORS(app)
在本地,这个flask_cors
包可以工作,但当部署到云函数上时就不行了。
正如GCP所建议的那样,我尝试了许多不同的方法https://cloud.google.com/functions/docs/writing/http但我仍然收到CORS错误:
对飞行前请求的响应未通过访问控制检查:请求的资源上不存在"access control Allow Origin"标头。
否,app
变量在云函数中不可用。
相反,您可以手动处理CORS:
def cors_enabled_function(request):
# For more information about CORS and CORS preflight requests, see
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
# for more information.
# Set CORS headers for the preflight request
if request.method == 'OPTIONS':
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '3600'
}
return ('', 204, headers)
# Set CORS headers for the main request
headers = {
'Access-Control-Allow-Origin': '*'
}
return ('Hello World!', 200, headers)
请参阅https://cloud.google.com/functions/docs/writing/http#handling_cors_requests了解更多详细信息。
如果您已经在使用flask
,那么最简单的方法是使用flask-cors
https://github.com/corydolphin/flask-cors
像下面这样装饰你的云功能,你就完成了。
from flask_cors import cross_origin
@cross_origin()
@json
def fun_function(request):
# enter code here
或者,您可以根据需要添加任意多的功能,如下所示。
from flask_cors import cross_origin
@cross_origin(allowed_methods=['POST'])
@json
def fun_function(request):
# enter code here
云函数中没有APP
。您可以按照谷歌云文档中的说明设置CORS头,并按照您在Flask中的编写方式返回JSON。
下面的示例函数名为hello_world
,用于发布请求。它返回CORS
的状态和标头。
from flask import jsonify
def hello_world(request):
request_json = request.get_json()
# Set CORS headers for the preflight request
if request.method == 'OPTIONS':
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '3600'
}
return ('', 204, headers)
# Set CORS headers for the main request
headers = {
'Access-Control-Allow-Methods': 'POST',
'Access-Control-Allow-Origin': '*'
}
if request_json and 'labels' in request_json:
# THIS IS THE PLACE YOU WRITE YOUR CODE.
# AWLAYS RETURN WITH THE HEADERS AND STATUS
return (jsonify({"ok": "Great Day 2"}), 200, headers)
@mdev,我遇到了类似的问题,并通过在CORS_enabled_function的开头添加预燃请求的CORS头来解决它(正如Dustin Ingram所建议的(;我在函数结束时留下的主请求的CORS头(这种方式包括返回语句中的响应,无论是JSON、文本等(。换句话说,我把我的主函数代码放在预触发和主CORS请求之间。
如果您正试图从前端对google cloud function
进行Post request
,但遇到CORS
问题。以下模板必须有效。它对我有效…
#import all required packages
import json
def function_name(request):
if request.method == 'OPTIONS':
headers = {
'Access-Control-Allow-Origin': '*', # Allow your function to be called from any domain
'Access-Control-Allow-Methods': 'POST', # POST or any method type you need to call
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '3600',
}
return ('', 204, headers)
# Set CORS headers for main requests
headers = {
'Access-Control-Allow-Origin': '*',
}
# Your code logic goes here
# Return your response
return (json.dumps({'status': 'success'}), 200, headers)
如果您还想在飞行前请求后处理Auth-in功能,请参阅以下讨论:https://github.com/FirebaseExtended/reactfire/discussions/483