我刚刚在 Beta Python 3.7 运行时中使用 HTTP 触发器编写了 Google Cloud Function。现在我试图弄清楚如何在调用函数时将字符串变量传递给函数。我已阅读文档,但没有找到任何相关内容。
我的触发器类似于:
https://us-central1-*PROJECT_ID*.cloudfunctions.net/*FUNCTION_NAME*
我是否误解了云功能的工作原理?你甚至可以将变量传递给他们吗?
Answer recommended by
Google Cloud /collectives/google-cloud
Collective
您可以将变量传递给函数,就像将变量传递给任何 URL 一样:
1. 通过GET
带查询参数:
def test(request):
name = request.args.get('name')
return f"Hello {name}"
$ curl -X GET https://us-central1-<PROJECT>.cloudfunctions.net/test?name=World
Hello World
2. 通过POST
具有以下形式:
def test(request):
name = request.form.get('name')
return f"Hello {name}"
$ curl -X POST https://us-central1-<PROJECT>.cloudfunctions.net/test -d "name=World"
Hello World
3. 通过POST
使用 JSON:
def test(request):
name = request.get_json().get('name')
return f"Hello {name}"
$ curl -X POST https://us-central1-<PROJECT>.cloudfunctions.net/test -d '{"name":"World"}'
Hello World
更多详情可在这找到:https://cloud.google.com/functions/docs/writing/http https://cloud.google.com/functions/docs/writing/http
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)