0

I want to call a Node-RED flow from IBM Cloud Functions.

const https = require('https');

function main(params) {
const path = "/" + params.route + "?" + params.query_params ;
const options = {
    hostname: params.hostname,
    path: path,
    port: 443,
    method: 'GET'
};

return new Promise((resolve, reject) => {
    https.get(options, (resp) => {
        resp.on('data', (d) => {
            let s = d.toString();
            obj = JSON.parse(s);
            resolve({ "gw_result": obj })
        });
    });
})

}

In the Node-RED flow I'm using a HTTP request to get data from another server. For test purposes I used a GET request to google.com but have same results using another Node-RED endpoint.

enter image description here

As soon as I invoke the web action I get the error message "The action did not produce a valid response and exited unexpectedly". The output of the Node-RED flow appears some seconds later in the web action's log although the Node-RED flow works properly and promptly (I used debug Node-RED debug nodes to check this).

The https GET request to Node-RED works well when I replace the http request in Node-RED by something else, e.g. a Function node, even when I use a Delay node to delay the response for a second or so.

jpsstack
  • 1,221
  • 4
  • 18
  • 29

1 Answers1

0

This code works, although google.com does not return an object, of course.

var rp = require('request-promise');
function main(params) {
    var uri = params.hostname + params.route + params.query_params
    return new Promise(function (resolve, reject) {
        rp(uri)
        .then(function (parsedBody) {
            obj = JSON.parse(parsedBody);
            resolve({ "gw_result": obj  

            });
        })
        .catch(function (err) {
            resolve({ message: 'failed!!', error: err.toString() });
        });
    });
}
jpsstack
  • 1,221
  • 4
  • 18
  • 29