将变量传递给 Google Cloud 函数 [英] Passing Variables To Google Cloud Functions
问题描述
我刚刚在 Beta Python 3.7 运行时中使用 HTTP 触发器完成了 Google Cloud Function 的编写.现在我想弄清楚在调用它时如何将字符串变量传递给我的函数.我已经阅读了文档,但我没有找到任何关于此的内容.
I've just finished writing a Google Cloud Function in the Beta Python 3.7 runtime with an HTTP trigger. Now I'm trying to figure out how to pass a string variable to my function when calling it. I've read the documentation but I haven't found anything on this.
我的触发器类似于:
https://us-central1-*PROJECT_ID*.cloudfunctions.net/*FUNCTION_NAME*
我是否误解了 Cloud Functions 的工作原理?您甚至可以将变量传递给它们吗?
Am I misunderstanding how Cloud Functions work? Can you even pass variables to them?
推荐答案
将变量传递给函数的方式与将变量传递给任何 URL 的方式相同:
You'd pass variables to the function the same way you'd pass variables to any URL:
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.通过带有 JSON 的 POST
:
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
More details can be found here: https://cloud.google.com/functions/docs/writing/http
这篇关于将变量传递给 Google Cloud 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!