我刚刚在Beta Python 3.7运行时编写了一个带有HTTP触发器的Google Cloud函数.现在我想知道调用函数时如何将字符串变量传递给函数.我已经看过文档了,但还没有找到任何相关信息.
我的触发器类似于:
https://us-central1-*PROJECT_ID*.cloudfunctions.net/*FUNCTION_NAME*
我是否误解了云功能的工作原理?你能把变量传递给他们吗?
我刚刚在Beta Python 3.7运行时编写了一个带有HTTP触发器的Google Cloud函数.现在我想知道调用函数时如何将字符串变量传递给函数.我已经看过文档了,但还没有找到任何相关信息.
我的触发器类似于:
https://us-central1-*PROJECT_ID*.cloudfunctions.net/*FUNCTION_NAME*
我是否误解了云功能的工作原理?你能把变量传递给他们吗?
将变量传递给函数的方式与将变量传递给任何URL的方式相同:
GET
with query parameters: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
POST
with a form: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
POST
with 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