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?
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.
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.
You'd pass variables to the function the same way you'd pass variables to any 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
More details can be found here: https://cloud.google.com/functions/docs/writing/http
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With