Passing Variables To Google Cloud Functions

前端 未结 1 1773
我在风中等你
我在风中等你 2020-12-01 17:14

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 f

相关标签:
1条回答
  • 2020-12-01 17:54

    You'd pass variables to the function the same way you'd pass variables to any URL:

    1. Via a 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
    

    2. Via a 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
    

    3. Via a 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
    

    More details can be found here: https://cloud.google.com/functions/docs/writing/http

    0 讨论(0)
提交回复
热议问题