Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom parameters/queries/headers to a Google Cloud function

I am basically wondering if I can have an https Google Cloud function execute differently based on what I pass to it as a parameter (like any normal function with parameters). Possibly with a simple query inside the URL or any other method that works.

As an example, taking a look at the basic starter function in any tutorial:

exports.helloWorld = functions.https.onRequest((req, res) => {
  res.status(200).send('Hello, World!');
});

I would like to do something like:

exports.helloWorld = functions.https.onRequest((req, res) => {
  let name = req.query;
  res.status(200).send(`Hello, ${name}!`);
});

I hope this makes sense. Any help would be appreciated.

like image 900
Christiaan Louw Avatar asked Nov 20 '25 02:11

Christiaan Louw


1 Answers

Indeed it is possible to pass various parameters to your Cloud Function.

Here's a simple example for your reference:

index.js

exports.helloWorld = (req, res) => {
  let name = req.body.name || req.query.name;
  res.status(200).send(`Hello ${name}!`);
};

I can call it via the URL provided for triggering the function, by appending that URL with /?name=<desired-name>:

https://<function-region>-<project-name>.cloudfunctions.net/<function-name>/?name=Christiaan

Alternatively, it can also be called using curl:

curl --data "name=Christiaan" https://<functions-region>-<project-name>.cloudfunctions.net/<function-name>

Output:

Hello Christiaan!
like image 72
Deniss T. Avatar answered Nov 22 '25 15:11

Deniss T.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!