Get Reqeust fails with Error: connect ECONNREFUSED












0















I'm trying to send a get request to the server. But I'm getting the following error, any idea how to avoid the error?



the error:




events.js:183
throw er; // Unhandled 'error' event
^



Error: connect ECONNREFUSED ******
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)




my request:



var https = require("https");

// Update these options with the details of the web service you would like to call
var options = {
method: 'GET',
uri: 'https:*****',
resolveWithFullResponse: true,
json: true
};

var req = https.request(options, res => {
res.setEncoding('utf8');
var returnData = "";

res.on('data', chunk => {
returnData = returnData + chunk;
});
res.on('end', () => {
console.log('returndata: ' + JSON.stringify(returnData))
var pop = JSON.parse(returnData).population;
callback(pop);
});
});
req.end();









share|improve this question





























    0















    I'm trying to send a get request to the server. But I'm getting the following error, any idea how to avoid the error?



    the error:




    events.js:183
    throw er; // Unhandled 'error' event
    ^



    Error: connect ECONNREFUSED ******
    at Object._errnoException (util.js:992:11)
    at _exceptionWithHostPort (util.js:1014:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)




    my request:



    var https = require("https");

    // Update these options with the details of the web service you would like to call
    var options = {
    method: 'GET',
    uri: 'https:*****',
    resolveWithFullResponse: true,
    json: true
    };

    var req = https.request(options, res => {
    res.setEncoding('utf8');
    var returnData = "";

    res.on('data', chunk => {
    returnData = returnData + chunk;
    });
    res.on('end', () => {
    console.log('returndata: ' + JSON.stringify(returnData))
    var pop = JSON.parse(returnData).population;
    callback(pop);
    });
    });
    req.end();









    share|improve this question



























      0












      0








      0








      I'm trying to send a get request to the server. But I'm getting the following error, any idea how to avoid the error?



      the error:




      events.js:183
      throw er; // Unhandled 'error' event
      ^



      Error: connect ECONNREFUSED ******
      at Object._errnoException (util.js:992:11)
      at _exceptionWithHostPort (util.js:1014:20)
      at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)




      my request:



      var https = require("https");

      // Update these options with the details of the web service you would like to call
      var options = {
      method: 'GET',
      uri: 'https:*****',
      resolveWithFullResponse: true,
      json: true
      };

      var req = https.request(options, res => {
      res.setEncoding('utf8');
      var returnData = "";

      res.on('data', chunk => {
      returnData = returnData + chunk;
      });
      res.on('end', () => {
      console.log('returndata: ' + JSON.stringify(returnData))
      var pop = JSON.parse(returnData).population;
      callback(pop);
      });
      });
      req.end();









      share|improve this question
















      I'm trying to send a get request to the server. But I'm getting the following error, any idea how to avoid the error?



      the error:




      events.js:183
      throw er; // Unhandled 'error' event
      ^



      Error: connect ECONNREFUSED ******
      at Object._errnoException (util.js:992:11)
      at _exceptionWithHostPort (util.js:1014:20)
      at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)




      my request:



      var https = require("https");

      // Update these options with the details of the web service you would like to call
      var options = {
      method: 'GET',
      uri: 'https:*****',
      resolveWithFullResponse: true,
      json: true
      };

      var req = https.request(options, res => {
      res.setEncoding('utf8');
      var returnData = "";

      res.on('data', chunk => {
      returnData = returnData + chunk;
      });
      res.on('end', () => {
      console.log('returndata: ' + JSON.stringify(returnData))
      var pop = JSON.parse(returnData).population;
      callback(pop);
      });
      });
      req.end();






      javascript node.js get-request






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday









      Yoram de Langen

      3,90511727




      3,90511727










      asked 2 days ago









      Germany DeutschlandGermany Deutschland

      368




      368
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Here's an example, you can't set uri on the options object as such, you either use a uri string / URI object or you use the options object for this request type (see Node.js http.request docs)



          const https = require('https');

          const options = {
          hostname: 'jsonplaceholder.typicode.com',
          path: '/todos/1',
          };

          const req = https.request(options, (res) => {
          let returnData = "";

          res.on('data', chunk => {
          returnData = returnData + chunk;
          });

          res.on('end', () => {
          console.log('returndata: ', returnData);
          });
          });

          req.on('error', (e) => {
          console.error(e);
          });

          req.end();





          share|improve this answer































            0














            There are two ways to define request options to be passed to https.request():





            1. By specifying hostname, path and (optionally) port as separate request options:



              var options = {
              method: 'GET',
              hostname: 'jsonplaceholder.typicode.com',
              path: '/posts/1',
              port: 443
              };



            2. By using creating an instance of url.URL:



              var URL = require('url').URL;
              var options = new URL('https://jsonplaceholder.typicode.com/posts/1');


              For more information please refer to the official documentation.








            share|improve this answer

























              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%2f54252784%2fget-reqeust-fails-with-error-connect-econnrefused%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's an example, you can't set uri on the options object as such, you either use a uri string / URI object or you use the options object for this request type (see Node.js http.request docs)



              const https = require('https');

              const options = {
              hostname: 'jsonplaceholder.typicode.com',
              path: '/todos/1',
              };

              const req = https.request(options, (res) => {
              let returnData = "";

              res.on('data', chunk => {
              returnData = returnData + chunk;
              });

              res.on('end', () => {
              console.log('returndata: ', returnData);
              });
              });

              req.on('error', (e) => {
              console.error(e);
              });

              req.end();





              share|improve this answer




























                0














                Here's an example, you can't set uri on the options object as such, you either use a uri string / URI object or you use the options object for this request type (see Node.js http.request docs)



                const https = require('https');

                const options = {
                hostname: 'jsonplaceholder.typicode.com',
                path: '/todos/1',
                };

                const req = https.request(options, (res) => {
                let returnData = "";

                res.on('data', chunk => {
                returnData = returnData + chunk;
                });

                res.on('end', () => {
                console.log('returndata: ', returnData);
                });
                });

                req.on('error', (e) => {
                console.error(e);
                });

                req.end();





                share|improve this answer


























                  0












                  0








                  0







                  Here's an example, you can't set uri on the options object as such, you either use a uri string / URI object or you use the options object for this request type (see Node.js http.request docs)



                  const https = require('https');

                  const options = {
                  hostname: 'jsonplaceholder.typicode.com',
                  path: '/todos/1',
                  };

                  const req = https.request(options, (res) => {
                  let returnData = "";

                  res.on('data', chunk => {
                  returnData = returnData + chunk;
                  });

                  res.on('end', () => {
                  console.log('returndata: ', returnData);
                  });
                  });

                  req.on('error', (e) => {
                  console.error(e);
                  });

                  req.end();





                  share|improve this answer













                  Here's an example, you can't set uri on the options object as such, you either use a uri string / URI object or you use the options object for this request type (see Node.js http.request docs)



                  const https = require('https');

                  const options = {
                  hostname: 'jsonplaceholder.typicode.com',
                  path: '/todos/1',
                  };

                  const req = https.request(options, (res) => {
                  let returnData = "";

                  res.on('data', chunk => {
                  returnData = returnData + chunk;
                  });

                  res.on('end', () => {
                  console.log('returndata: ', returnData);
                  });
                  });

                  req.on('error', (e) => {
                  console.error(e);
                  });

                  req.end();






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 days ago









                  Terry LennoxTerry Lennox

                  4,6752617




                  4,6752617

























                      0














                      There are two ways to define request options to be passed to https.request():





                      1. By specifying hostname, path and (optionally) port as separate request options:



                        var options = {
                        method: 'GET',
                        hostname: 'jsonplaceholder.typicode.com',
                        path: '/posts/1',
                        port: 443
                        };



                      2. By using creating an instance of url.URL:



                        var URL = require('url').URL;
                        var options = new URL('https://jsonplaceholder.typicode.com/posts/1');


                        For more information please refer to the official documentation.








                      share|improve this answer






























                        0














                        There are two ways to define request options to be passed to https.request():





                        1. By specifying hostname, path and (optionally) port as separate request options:



                          var options = {
                          method: 'GET',
                          hostname: 'jsonplaceholder.typicode.com',
                          path: '/posts/1',
                          port: 443
                          };



                        2. By using creating an instance of url.URL:



                          var URL = require('url').URL;
                          var options = new URL('https://jsonplaceholder.typicode.com/posts/1');


                          For more information please refer to the official documentation.








                        share|improve this answer




























                          0












                          0








                          0







                          There are two ways to define request options to be passed to https.request():





                          1. By specifying hostname, path and (optionally) port as separate request options:



                            var options = {
                            method: 'GET',
                            hostname: 'jsonplaceholder.typicode.com',
                            path: '/posts/1',
                            port: 443
                            };



                          2. By using creating an instance of url.URL:



                            var URL = require('url').URL;
                            var options = new URL('https://jsonplaceholder.typicode.com/posts/1');


                            For more information please refer to the official documentation.








                          share|improve this answer















                          There are two ways to define request options to be passed to https.request():





                          1. By specifying hostname, path and (optionally) port as separate request options:



                            var options = {
                            method: 'GET',
                            hostname: 'jsonplaceholder.typicode.com',
                            path: '/posts/1',
                            port: 443
                            };



                          2. By using creating an instance of url.URL:



                            var URL = require('url').URL;
                            var options = new URL('https://jsonplaceholder.typicode.com/posts/1');


                            For more information please refer to the official documentation.









                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited yesterday

























                          answered 2 days ago









                          antonkuantonku

                          6796




                          6796






























                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54252784%2fget-reqeust-fails-with-error-connect-econnrefused%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

                              Liquibase includeAll doesn't find base path

                              How to use setInterval in EJS file?

                              Petrus Granier-Deferre