Cloud Functions triggered by HTTP

HTTP cloud functions are used to invoke your functions through HTTP requests. In an HTTP function, the request parameter represents any call action sent to functions, while response represents return values:

  1. Write the Cloud Function and save it as index.js:
/**
* Responds to any HTTP request that can provide a "message" field in the body.
*/
exports.helloWorld = function helloHttp (req, res) {
if (req.body.message === undefined) {
// An error case because a "message" is missing
res.status(400).send('No message defined!');
} else {
// Everything is ok
console.log(req.body.message);
res.status(200).end();
}
};
  1. Deploy the Cloud Function:
gcloud beta functions deploy helloHttp --trigger-http  
  1. Invoke the Cloud Function using Client URL (curl). The curl command can be used to call the function and pass a simple message. Curl is a command-line utility that allows us to send or receive HTTP requests:
curl -X POST -H "Content-Type:application/json" -d '{"message":"hello world!"}' YOUR_HTTP_TRIGGER_ENDPOINT

Your HTTP endpoint is typically the json schema that you wrote to invoke your functions via HTTP.  In other words, YOUR_HTTP_TRIGGER_ENDPOINT reported by the previous command is used to create the function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset