为了账号安全,请及时绑定邮箱和手机立即绑定

启用 CORS Google Cloud Function (Python)

启用 CORS Google Cloud Function (Python)

弑天下 2023-09-12 16:29:29
可以flask_cors在 Google Cloud Functions 中使用吗?app = Flask(__name__) cors = CORS(app)该flask_cors包在本地有效,但部署到云功能上时却无效。我尝试了很多不同的方法,正如 GCP 建议的那样https://cloud.google.com/functions/docs/writing/http 但我仍然收到 CORS 错误:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。
查看完整描述

5 回答

?
慕森王

TA贡献1777条经验 获得超3个赞

不可以,该app变量在 Cloud Functions 中不可用。


相反,您可以手动处理 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)

查看完整回答
反对 回复 2023-09-12
?
catspeake

TA贡献1111条经验 获得超0个赞

APP云功能中没有。您可以按照google cloud 文档中的说明设置 CORS 标头,并按照在 Flask 中编写的方式返回 JSON。

下面的示例调用了hello_world用于 post 请求的函数。它返回 的状态和标头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)


查看完整回答
反对 回复 2023-09-12
?
月关宝盒

TA贡献1772条经验 获得超5个赞

如果您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


查看完整回答
反对 回复 2023-09-12
?
猛跑小猪

TA贡献1858条经验 获得超8个赞

并通过在 cors_enabled_function 的开头添加预检请求的 CORS 标头来解决它(正如达斯汀·英格拉姆(Dustin Ingram)建议的那样);我留在函数末尾的主请求的 CORS 标头(这样包括返回语句中的响应,无论是 JSON、文本等)。换句话说,我将主函数代码放在预检请求和主 CORS 请求之间。

查看完整回答
反对 回复 2023-09-12
?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

如果您尝试从前端进行操作Post request并google cloud function遇到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)

查看完整回答
反对 回复 2023-09-12
  • 5 回答
  • 0 关注
  • 108 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信