Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Variables To Google Cloud Functions

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.

My trigger resembles:

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

Am I misunderstanding how Cloud Functions work? Can you even pass variables to them?

like image 945
pinglock Avatar asked Sep 08 '18 09:09

pinglock


People also ask

How can we pass parameters to workflow?

You can pass a different set of parameter values each time you run a parameterized workflow template. You must provide a value for each parameter defined in the template. You can pass a map of parameter names to values to the gcloud dataproc workflow-templates instantiate command with the --parameters flag.

What is entry point in Google Cloud function?

The entry point is defined during the function deployment. As you can see in the screenshot in the answer you have linked, you have to specify the name of the function. If you are using the console then you have to specify the entry point with --entry-point flag.


1 Answers

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

like image 200
Dustin Ingram Avatar answered Oct 09 '22 09:10

Dustin Ingram