2

How do I write a Google Cloud Function that will receive a HTTP request and then send a HTTP POST request to a different endpoint?

For example,

By far I have tried sending HTTP POST with node-webhooks, request & restler modules but none of them seem to work. Is it because these modules are used in conjunction with exports.test ?

My question is related to this question but the answers didn't help me.

The data being sent to endpoint is in json & Content-type: application/json.

var request = require('request'); //also tried for node-webhook, restler modules

exports.test = function(req, res) {
 //processing of received json data from source A. 
}

function sendToEndpoint(processed_data) {

  let abc = processed_data;   //send processed data to source B 

  request.post({
      uri: 'https://example.com',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify(abc)
  }); 

}
Electric_90
  • 161
  • 1
  • 11
  • Possible duplicate of [Google Cloud function to fetch data from third party server](https://stackoverflow.com/questions/54111202/google-cloud-function-to-fetch-data-from-third-party-server) – Renaud Tarnec Apr 24 '19 at 18:29
  • @RenaudTarnec, no my question is not related to that. I have no problem in receiving the data. I am facing trouble in making a HTTP(s) POST request from cloud function. – Electric_90 Apr 24 '19 at 18:32
  • This is exactly what this answer is covering: how to issue a POST HTPP Request to an external endpoint, **from the Cloud Function**. – Renaud Tarnec Apr 24 '19 at 18:34
  • Please show some code you tried and what was the result. – Molda Apr 24 '19 at 18:34
  • @RenaudTarnec, As I mentioned, I have already tried those modules but they do not seem to work. – Electric_90 Apr 24 '19 at 18:39
  • @Electric_90 Sorry to insist, but the code in this answer does work. You will note that the the node.js request-promise library is used, which is a "simplified HTTP request client 'request' with Promise support". You may publish the code you have tried if you need more help. – Renaud Tarnec Apr 24 '19 at 18:45
  • @RenaudTarnec, thanks for your help, Is there some kind of alteration need to be made in cloud function's settings for webhook to work? I deployed my cloud function with gcloud beta functions. – Electric_90 Apr 24 '19 at 18:58
  • 1
    @Electric_90 To be honest I don't know... my experience with Cloud Functions is limited to Firebase ones, deployed through the Firebase CLI. But from what I know, there shouldn't be any difference in this case. Only point I cazn see is that with Firebase CF, you need to be on the "Flame" or "Blaze" pricing plan. – Renaud Tarnec Apr 24 '19 at 19:02

3 Answers3

2

As @RenaudTarnec mentioned in the comments the problem was that my billing account was not configured.

Google doesn't allow outbound requests to external servers without valid billing info so as to prevent any malicious activity.

After configuring a billing account I was able to make outbound requests and all the node-modules mentioned in the question worked.

Electric_90
  • 161
  • 1
  • 11
1

Within a Cloud Function that is written in Node.js, you can use any package that is accessible via NPM. Using Google Search to find such packages shows:

Which one you choose is usually a matter of taste. My personal choice is to use the one which does the job and is most popular. I equate popularity with liklihood of on-going support and updates.

Kolban
  • 13,794
  • 3
  • 38
  • 60
0

Use Websockets SOCKET.IO is the best choice. https://socket.io/

However it is impossible to use this in cloud functions. Because it is distributed in different machines. So keeping track is impossibe.