[英]Enabling CORS Python in Google Cloud Platform Function
我已经关注了一些 Medium 文章以及 Google 自己的教程,但仍然无法在此 function 上使用 flask 启用 CORS:(
我缺少什么代码来启用 CORS? 我想要 * 的通配符,因为我希望所有服务/域都能够访问此功能和其他功能。
我听说 GCP 不适用于 @app。 为什么,我不太确定。
请注意,我已经成功地直接在浏览器的 url 栏上测试输出,并在 GCP 本身内触发 JSON 测试。
我的预期用途仅用于 GET 请求。
from flask import Flask
import re
def hello_method(request):
from flask import abort
if request.method == 'GET':
headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '3600'
}
request_json = request.get_json()
pattern = re.compile("([0-9]+(\.[0-9]+)?)")
if request.args and 'x' and 'y' in request.args:
x_str = request.args.get('x')
y_str = request.args.get('y')
if not (pattern.match(x_str)):
rtn = "{\"error\":true,\"string\":" + "NaN" + "*" + y_str + "=" + "NaN" + ",\"answer\":" + "NaN" +"}"
return (rtn, 204, headers)
if not (pattern.match(y_str)):
rtn = "{\"error\":true,\"string\":" + x_str + "*" + "NaN" + "=" + "NaN" + ",\"answer\":" + "NaN" +"}"
return (rtn, 204, headers)
x = float(x_str)
y = float(y_str)
print("Args X: " + str(x))
print("Args Y: " + str(y))
ans = x * y
print("Args Answer: " + str(ans))
rtn = "{\"error\":false,\"string\":" + str(x) + "*" + str(y) + "=" + str(ans) + ",\"answer\":" + str(ans) +"}"
return (rtn, 204, headers)
elif request_json and 'x' and 'y' in request_json:
x_str = request.args.get('x')
y_str = request.args.get('y')
if not (pattern.match(x_str)):
rtn = "{\"error\":true,\"string\":" + "NaN" + "*" + y_str + "=" + "NaN" + ",\"answer\":" + "NaN" +"}"
return (rtn, 204, headers)
if not (pattern.match(y_str)):
rtn = "{\"error\":true,\"string\":" + x_str + "*" + "NaN" + "=" + "NaN" + ",\"answer\":" + "NaN" +"}"
return (rtn, 204, headers)
print("JSON: ", request_json)
x = float(request_json['x'])
y = float(request_json['y'])
print("JSON X: ", str(x))
print("JSON Y: ", str(y))
ans = x * y
print("JSON Answer 2: " + str(ans))
rtn = "{\"error\":false,\"string\":" + str(x) + "*" + str(y) + "=" + str(ans) + ",\"answer\":" + str(ans) +"}"
return (rtn, 204, headers)
else:
return f"Please pass 2 variables x and y as http"
elif request.method == 'PUT':
return abort(403)
else:
return abort(405)
我似乎知道 CORS 的理论,但我的实践还不够强大,而且 GCP 似乎以不同的方式与 flask-cors 一起工作。
首先,不要使用 Flask。 每个 function 都旨在成为单个操作,Cloud Functions 已经负责请求路由和框架处理。
至于 CORS 问题,Cloud Functions 文档中提供了有关如何处理 CORS 的示例。
在更新的代码中,您拒绝 PUT 请求并在 GET 方法而不是 OPTIONS 方法中返回 CORS 标头。 这不是上面链接的文档所做的,请仔细检查。 Cloud Function 的架构应如下所示:
def the_function(request):
# Return CORS headers on OPTIONS request.
if request.method == 'OPTIONS':
headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '3600'
}
return ('', 204, headers)
# If the request is GET then handle it normally
if request.method == 'GET':
x = request.args('x')
y = request.args('x')
result = int(x) * int(y)
headers = {
'Access-Control-Allow-Origin': '*'
}
return (result, 200, headers)
else:
# If the request is not GET or OPTIONS, deny.
return '', 400
我了解您只想允许对 function 的 GET 请求。 但是,必须允许 OPTIONS HTTP 方法才能使 CORS 正常工作。 当网页向另一个域请求内容时,浏览器可能会向目标域(在这种情况下为 Cloud Function)发送预检请求,询问是否允许实际的 GET/POST/etc.. 请求。 为了实现这一点,一个 OPTIONS 请求被发送到服务器,服务器必须响应确认允许的 HTTP 方法。 因此,要让 function 按预期工作,它必须:
Access-Control-Allow-XXX
标头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.