首页 文章

将变量传递给Google Cloud Functions

提问于
浏览
1

我刚刚使用HTTP触发器在Beta Python 3.7运行时编写了一个Google Cloud Function . 现在我想弄清楚如何在调用它时将字符串变量传递给我的函数 . 我已阅读文档,但我没有找到任何相关内容 .

我的触发器类似于:

https://us-central1-*PROJECT_ID*.cloudfunctions.net/*FUNCTION_NAME*

我误解了 Cloud 功能的工作原理吗?你甚至可以将变量传递给他们吗?

1 回答

  • 3

    您将变量传递给函数的方式与将变量传递给任何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.通过带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

相关问题