Use Request Module HTTP GET Firebase Function Node.js












0














I am using Firebase Cloud functions priced plan (!) for a webhook for google actions. I am trying to implement a HTTP GET with the request-module. I have installed the module with npm install request and deployed correctly. I want to use google maps distance-matrix api.



For some reasons I am not able to use the request module at all. In the log I neither see "ERROR in GET" nor "being SUCCESSFULL". Any idea what the issue might be?



// Handle the Dialogflow intent named 'Default Welcome Intent'.
app.intent('Default Welcome Intent', (conv) => {

var url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Washington,DC&destinations=New+York+City,NY&key=AIzaSyBich-7OBAxvtAwX5XnHQyJ7xZiJ8libVQ";

request({url: url, json: true}, (err, resp, body) => {
if (err) {
console.log('ERROR in GET');
conv.ask('ERROR in GET');
}
else {
conv.ask('beeing SUCCESSFULL');
console.log('beeing SUCCESSFULL');
}
})


Thank you in advance and best regards.
OliDev










share|improve this question





























    0














    I am using Firebase Cloud functions priced plan (!) for a webhook for google actions. I am trying to implement a HTTP GET with the request-module. I have installed the module with npm install request and deployed correctly. I want to use google maps distance-matrix api.



    For some reasons I am not able to use the request module at all. In the log I neither see "ERROR in GET" nor "being SUCCESSFULL". Any idea what the issue might be?



    // Handle the Dialogflow intent named 'Default Welcome Intent'.
    app.intent('Default Welcome Intent', (conv) => {

    var url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Washington,DC&destinations=New+York+City,NY&key=AIzaSyBich-7OBAxvtAwX5XnHQyJ7xZiJ8libVQ";

    request({url: url, json: true}, (err, resp, body) => {
    if (err) {
    console.log('ERROR in GET');
    conv.ask('ERROR in GET');
    }
    else {
    conv.ask('beeing SUCCESSFULL');
    console.log('beeing SUCCESSFULL');
    }
    })


    Thank you in advance and best regards.
    OliDev










    share|improve this question



























      0












      0








      0







      I am using Firebase Cloud functions priced plan (!) for a webhook for google actions. I am trying to implement a HTTP GET with the request-module. I have installed the module with npm install request and deployed correctly. I want to use google maps distance-matrix api.



      For some reasons I am not able to use the request module at all. In the log I neither see "ERROR in GET" nor "being SUCCESSFULL". Any idea what the issue might be?



      // Handle the Dialogflow intent named 'Default Welcome Intent'.
      app.intent('Default Welcome Intent', (conv) => {

      var url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Washington,DC&destinations=New+York+City,NY&key=AIzaSyBich-7OBAxvtAwX5XnHQyJ7xZiJ8libVQ";

      request({url: url, json: true}, (err, resp, body) => {
      if (err) {
      console.log('ERROR in GET');
      conv.ask('ERROR in GET');
      }
      else {
      conv.ask('beeing SUCCESSFULL');
      console.log('beeing SUCCESSFULL');
      }
      })


      Thank you in advance and best regards.
      OliDev










      share|improve this question















      I am using Firebase Cloud functions priced plan (!) for a webhook for google actions. I am trying to implement a HTTP GET with the request-module. I have installed the module with npm install request and deployed correctly. I want to use google maps distance-matrix api.



      For some reasons I am not able to use the request module at all. In the log I neither see "ERROR in GET" nor "being SUCCESSFULL". Any idea what the issue might be?



      // Handle the Dialogflow intent named 'Default Welcome Intent'.
      app.intent('Default Welcome Intent', (conv) => {

      var url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Washington,DC&destinations=New+York+City,NY&key=AIzaSyBich-7OBAxvtAwX5XnHQyJ7xZiJ8libVQ";

      request({url: url, json: true}, (err, resp, body) => {
      if (err) {
      console.log('ERROR in GET');
      conv.ask('ERROR in GET');
      }
      else {
      conv.ask('beeing SUCCESSFULL');
      console.log('beeing SUCCESSFULL');
      }
      })


      Thank you in advance and best regards.
      OliDev







      node.js firebase google-cloud-functions google-assist-api






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 4:13









      bunbun

      2,03532346




      2,03532346










      asked Nov 11 at 21:49









      Oli Dev

      112




      112
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Here when you are doing async work inside handler function , you have to return a Promise that does that work , because otherwise there would be empty response and error. so



          app.intent('Default Welcome Intent', (conv) => {
          return Promise(function(resolve,reject){
          request({url: url, json: true}, (err, resp, body) => {
          if (err) {
          console.log('ERROR in GET');
          conv.ask('ERROR in GET');
          }else {
          conv.ask('beeing SUCCESSFULL');
          console.log('beeing SUCCESSFULL');
          }
          resolve()
          })
          })
          })


          Hope this works for you.






          share|improve this answer





















          • i get the Error: MalformedResponse 'final_response' must be set.
            – Oli Dev
            Nov 12 at 18:54










          • what does the firebase logs says ?, and does it logs console.log statements
            – siddhant sankhe
            Nov 13 at 6:57












          • Here you can see the error log. It does NOT log console.log statements. drive.google.com/file/d/1ziUdFBfn5QZugEVyLYBA_Ye7JyyW-qq1/…
            – Oli Dev
            Nov 14 at 10:31





















          0














          I think found a solution. Apparently it seems to be not possible to use conv.ask() within a promise for some reason. It is necessary to pass an argument. The following code does work:



          const p = new Promise(function(resolve,reject){
          request({url: 'YOUR_URL',
          json: true}, (err, resp, body) => {
          if (err) {
          // do not use conv.ask() here
          resolve('no ok');
          } else {
          // do not use conv.ask() here
          resolve('ok');
          }

          })
          })

          app.intent('Default Welcome Intent', (conv) => {

          p.then(resp => conv.ask('resp'));
          conv.ask('foobar');

          })





          share|improve this answer























          • well i have used the conv.ask inside promise and it works fine , but according to your code the Default Welcome Intent will always execute conv.ask('foobar'); as it will be executed and program wont wait for p.then() to resolve , so it will work but your purpose wont be fulfilled as it will always display 'foobar'
            – siddhant sankhe
            Nov 14 at 11:40












          • How did you do it then? Your proposed solution didn't work for me. Thank you in advance.
            – Oli Dev
            Nov 14 at 12:49












          • Can you please share img or code snippet of exact code that you are using right now , maybe i ca help you with that if possible , thanks
            – siddhant sankhe
            Nov 15 at 5:59













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253586%2fuse-request-module-http-get-firebase-function-node-js%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Here when you are doing async work inside handler function , you have to return a Promise that does that work , because otherwise there would be empty response and error. so



          app.intent('Default Welcome Intent', (conv) => {
          return Promise(function(resolve,reject){
          request({url: url, json: true}, (err, resp, body) => {
          if (err) {
          console.log('ERROR in GET');
          conv.ask('ERROR in GET');
          }else {
          conv.ask('beeing SUCCESSFULL');
          console.log('beeing SUCCESSFULL');
          }
          resolve()
          })
          })
          })


          Hope this works for you.






          share|improve this answer





















          • i get the Error: MalformedResponse 'final_response' must be set.
            – Oli Dev
            Nov 12 at 18:54










          • what does the firebase logs says ?, and does it logs console.log statements
            – siddhant sankhe
            Nov 13 at 6:57












          • Here you can see the error log. It does NOT log console.log statements. drive.google.com/file/d/1ziUdFBfn5QZugEVyLYBA_Ye7JyyW-qq1/…
            – Oli Dev
            Nov 14 at 10:31


















          0














          Here when you are doing async work inside handler function , you have to return a Promise that does that work , because otherwise there would be empty response and error. so



          app.intent('Default Welcome Intent', (conv) => {
          return Promise(function(resolve,reject){
          request({url: url, json: true}, (err, resp, body) => {
          if (err) {
          console.log('ERROR in GET');
          conv.ask('ERROR in GET');
          }else {
          conv.ask('beeing SUCCESSFULL');
          console.log('beeing SUCCESSFULL');
          }
          resolve()
          })
          })
          })


          Hope this works for you.






          share|improve this answer





















          • i get the Error: MalformedResponse 'final_response' must be set.
            – Oli Dev
            Nov 12 at 18:54










          • what does the firebase logs says ?, and does it logs console.log statements
            – siddhant sankhe
            Nov 13 at 6:57












          • Here you can see the error log. It does NOT log console.log statements. drive.google.com/file/d/1ziUdFBfn5QZugEVyLYBA_Ye7JyyW-qq1/…
            – Oli Dev
            Nov 14 at 10:31
















          0












          0








          0






          Here when you are doing async work inside handler function , you have to return a Promise that does that work , because otherwise there would be empty response and error. so



          app.intent('Default Welcome Intent', (conv) => {
          return Promise(function(resolve,reject){
          request({url: url, json: true}, (err, resp, body) => {
          if (err) {
          console.log('ERROR in GET');
          conv.ask('ERROR in GET');
          }else {
          conv.ask('beeing SUCCESSFULL');
          console.log('beeing SUCCESSFULL');
          }
          resolve()
          })
          })
          })


          Hope this works for you.






          share|improve this answer












          Here when you are doing async work inside handler function , you have to return a Promise that does that work , because otherwise there would be empty response and error. so



          app.intent('Default Welcome Intent', (conv) => {
          return Promise(function(resolve,reject){
          request({url: url, json: true}, (err, resp, body) => {
          if (err) {
          console.log('ERROR in GET');
          conv.ask('ERROR in GET');
          }else {
          conv.ask('beeing SUCCESSFULL');
          console.log('beeing SUCCESSFULL');
          }
          resolve()
          })
          })
          })


          Hope this works for you.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 6:47









          siddhant sankhe

          31439




          31439












          • i get the Error: MalformedResponse 'final_response' must be set.
            – Oli Dev
            Nov 12 at 18:54










          • what does the firebase logs says ?, and does it logs console.log statements
            – siddhant sankhe
            Nov 13 at 6:57












          • Here you can see the error log. It does NOT log console.log statements. drive.google.com/file/d/1ziUdFBfn5QZugEVyLYBA_Ye7JyyW-qq1/…
            – Oli Dev
            Nov 14 at 10:31




















          • i get the Error: MalformedResponse 'final_response' must be set.
            – Oli Dev
            Nov 12 at 18:54










          • what does the firebase logs says ?, and does it logs console.log statements
            – siddhant sankhe
            Nov 13 at 6:57












          • Here you can see the error log. It does NOT log console.log statements. drive.google.com/file/d/1ziUdFBfn5QZugEVyLYBA_Ye7JyyW-qq1/…
            – Oli Dev
            Nov 14 at 10:31


















          i get the Error: MalformedResponse 'final_response' must be set.
          – Oli Dev
          Nov 12 at 18:54




          i get the Error: MalformedResponse 'final_response' must be set.
          – Oli Dev
          Nov 12 at 18:54












          what does the firebase logs says ?, and does it logs console.log statements
          – siddhant sankhe
          Nov 13 at 6:57






          what does the firebase logs says ?, and does it logs console.log statements
          – siddhant sankhe
          Nov 13 at 6:57














          Here you can see the error log. It does NOT log console.log statements. drive.google.com/file/d/1ziUdFBfn5QZugEVyLYBA_Ye7JyyW-qq1/…
          – Oli Dev
          Nov 14 at 10:31






          Here you can see the error log. It does NOT log console.log statements. drive.google.com/file/d/1ziUdFBfn5QZugEVyLYBA_Ye7JyyW-qq1/…
          – Oli Dev
          Nov 14 at 10:31















          0














          I think found a solution. Apparently it seems to be not possible to use conv.ask() within a promise for some reason. It is necessary to pass an argument. The following code does work:



          const p = new Promise(function(resolve,reject){
          request({url: 'YOUR_URL',
          json: true}, (err, resp, body) => {
          if (err) {
          // do not use conv.ask() here
          resolve('no ok');
          } else {
          // do not use conv.ask() here
          resolve('ok');
          }

          })
          })

          app.intent('Default Welcome Intent', (conv) => {

          p.then(resp => conv.ask('resp'));
          conv.ask('foobar');

          })





          share|improve this answer























          • well i have used the conv.ask inside promise and it works fine , but according to your code the Default Welcome Intent will always execute conv.ask('foobar'); as it will be executed and program wont wait for p.then() to resolve , so it will work but your purpose wont be fulfilled as it will always display 'foobar'
            – siddhant sankhe
            Nov 14 at 11:40












          • How did you do it then? Your proposed solution didn't work for me. Thank you in advance.
            – Oli Dev
            Nov 14 at 12:49












          • Can you please share img or code snippet of exact code that you are using right now , maybe i ca help you with that if possible , thanks
            – siddhant sankhe
            Nov 15 at 5:59


















          0














          I think found a solution. Apparently it seems to be not possible to use conv.ask() within a promise for some reason. It is necessary to pass an argument. The following code does work:



          const p = new Promise(function(resolve,reject){
          request({url: 'YOUR_URL',
          json: true}, (err, resp, body) => {
          if (err) {
          // do not use conv.ask() here
          resolve('no ok');
          } else {
          // do not use conv.ask() here
          resolve('ok');
          }

          })
          })

          app.intent('Default Welcome Intent', (conv) => {

          p.then(resp => conv.ask('resp'));
          conv.ask('foobar');

          })





          share|improve this answer























          • well i have used the conv.ask inside promise and it works fine , but according to your code the Default Welcome Intent will always execute conv.ask('foobar'); as it will be executed and program wont wait for p.then() to resolve , so it will work but your purpose wont be fulfilled as it will always display 'foobar'
            – siddhant sankhe
            Nov 14 at 11:40












          • How did you do it then? Your proposed solution didn't work for me. Thank you in advance.
            – Oli Dev
            Nov 14 at 12:49












          • Can you please share img or code snippet of exact code that you are using right now , maybe i ca help you with that if possible , thanks
            – siddhant sankhe
            Nov 15 at 5:59
















          0












          0








          0






          I think found a solution. Apparently it seems to be not possible to use conv.ask() within a promise for some reason. It is necessary to pass an argument. The following code does work:



          const p = new Promise(function(resolve,reject){
          request({url: 'YOUR_URL',
          json: true}, (err, resp, body) => {
          if (err) {
          // do not use conv.ask() here
          resolve('no ok');
          } else {
          // do not use conv.ask() here
          resolve('ok');
          }

          })
          })

          app.intent('Default Welcome Intent', (conv) => {

          p.then(resp => conv.ask('resp'));
          conv.ask('foobar');

          })





          share|improve this answer














          I think found a solution. Apparently it seems to be not possible to use conv.ask() within a promise for some reason. It is necessary to pass an argument. The following code does work:



          const p = new Promise(function(resolve,reject){
          request({url: 'YOUR_URL',
          json: true}, (err, resp, body) => {
          if (err) {
          // do not use conv.ask() here
          resolve('no ok');
          } else {
          // do not use conv.ask() here
          resolve('ok');
          }

          })
          })

          app.intent('Default Welcome Intent', (conv) => {

          p.then(resp => conv.ask('resp'));
          conv.ask('foobar');

          })






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 at 12:51

























          answered Nov 14 at 11:03









          Oli Dev

          112




          112












          • well i have used the conv.ask inside promise and it works fine , but according to your code the Default Welcome Intent will always execute conv.ask('foobar'); as it will be executed and program wont wait for p.then() to resolve , so it will work but your purpose wont be fulfilled as it will always display 'foobar'
            – siddhant sankhe
            Nov 14 at 11:40












          • How did you do it then? Your proposed solution didn't work for me. Thank you in advance.
            – Oli Dev
            Nov 14 at 12:49












          • Can you please share img or code snippet of exact code that you are using right now , maybe i ca help you with that if possible , thanks
            – siddhant sankhe
            Nov 15 at 5:59




















          • well i have used the conv.ask inside promise and it works fine , but according to your code the Default Welcome Intent will always execute conv.ask('foobar'); as it will be executed and program wont wait for p.then() to resolve , so it will work but your purpose wont be fulfilled as it will always display 'foobar'
            – siddhant sankhe
            Nov 14 at 11:40












          • How did you do it then? Your proposed solution didn't work for me. Thank you in advance.
            – Oli Dev
            Nov 14 at 12:49












          • Can you please share img or code snippet of exact code that you are using right now , maybe i ca help you with that if possible , thanks
            – siddhant sankhe
            Nov 15 at 5:59


















          well i have used the conv.ask inside promise and it works fine , but according to your code the Default Welcome Intent will always execute conv.ask('foobar'); as it will be executed and program wont wait for p.then() to resolve , so it will work but your purpose wont be fulfilled as it will always display 'foobar'
          – siddhant sankhe
          Nov 14 at 11:40






          well i have used the conv.ask inside promise and it works fine , but according to your code the Default Welcome Intent will always execute conv.ask('foobar'); as it will be executed and program wont wait for p.then() to resolve , so it will work but your purpose wont be fulfilled as it will always display 'foobar'
          – siddhant sankhe
          Nov 14 at 11:40














          How did you do it then? Your proposed solution didn't work for me. Thank you in advance.
          – Oli Dev
          Nov 14 at 12:49






          How did you do it then? Your proposed solution didn't work for me. Thank you in advance.
          – Oli Dev
          Nov 14 at 12:49














          Can you please share img or code snippet of exact code that you are using right now , maybe i ca help you with that if possible , thanks
          – siddhant sankhe
          Nov 15 at 5:59






          Can you please share img or code snippet of exact code that you are using right now , maybe i ca help you with that if possible , thanks
          – siddhant sankhe
          Nov 15 at 5:59




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253586%2fuse-request-module-http-get-firebase-function-node-js%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          さくらももこ

          13 indicted, 8 arrested in Calif. drug cartel investigation