1

I have developed Google Cloud Function which calls an API hosted in AZURE. However the function returns error

Error: function crashed.Details: getaddrinfo ENOTFOUND https://bupanonproduction.azure-api.net https://bupanonproduction.azure-api.net:443

Below is the google cloud function

'use strict';
const http = require('https');
const host = 'https://bupanonproduction.azure-api.net';
exports.remaininglimits = (req, res) => {
  // Call the API
  callRemainingLimitsApi().then((output) => {
    // Return the results from the API to API.AI
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ 'speech': output, 'displayText': output }));
  }).catch((error) => {     
        // If there is an error let the user know
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ 'speech': error, 'displayText': error }));
  });
};
function callRemainingLimitsApi () {
  return new Promise((resolve, reject) => {
    // Create the path for the HTTP request to get the weather
    let path = '/api/Values';
    console.log('API Request: ' + host + path);
    // Make the HTTP request to get the weather
    http.get({host: host, path: path, headers: {'Ocp-Apim-Subscription-Key':'0a6e2fa822ec4d7a821d7f286abb6990'}}, (res) => {
         let body = ''; // var to store the response chunks
      res.on('data', (d) => { body += d; }); // store each response chunk
      res.on('end', () => {
        // After all the data has been received parse the JSON for desired data
        let response = JSON.parse(body);
        let jasonString = JSON.stringify(response);
        // Create response
        let output = `Hi, your limit is ${jasonString}.`;
        // Resolve the promise with the output text
        console.log(output);
        resolve(output);
      });
      res.on('error', (error) => {
        reject(error);
      });
    });
  });
}

When I use other public API such as below it returns correct result to the cloud function.

https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo

Any idea why the cloud function not recognizing the AZURE API url?

-Alan-

Nicholas
  • 501
  • 2
  • 14
Alan B
  • 2,219
  • 4
  • 32
  • 62
  • Possible duplicate of [Cloud Functions for Firebase - getaddrinfo ENOTFOUND](https://stackoverflow.com/questions/42774807/cloud-functions-for-firebase-getaddrinfo-enotfound) – Doug Stevenson Mar 06 '18 at 01:01
  • You need to be on a payment plan to send outgoing network traffic to destinations that are not controlled by Google. – Doug Stevenson Mar 06 '18 at 01:11
  • @DougStevenson, How this API (https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo) able to return results? – Alan B Mar 06 '18 at 03:54

1 Answers1

0

I just found out that the host should be defined without the prefix "https". That fixed the problem. I am using Free Trial with $300 credit and I am not sure if this is considered a paid plan.

Alan B
  • 2,219
  • 4
  • 32
  • 62