Peter Breen
Peter Breen

Reputation: 447

ECONNREFUSED, Node HTTP GET Request

I'm in a bit over my head trying to do a few things that are new to me. I'm building an API to interact with a Nationbuilder website using Node and AWS Lambda. I need to draw information from the database using Nationbuilder's API. I've tried a lot of variations in my code and this is my latest stripped-down attempt. It is returning "Error: connect ECONNREFUSED 127.0.0.1:443". Does anyone see what I should be doing differently? I replaced the access token with ******. The exports handler function is barely being used; it connects with Lambda's main functionality of handling requests instead of making them.

I can successfully make a GET request through hurl.it, so the problem is on my end.

var package = require('./package.json');
var myNewApi = require('./lib/my_new_api.js');
var http = require("http");
var https = require("https");

var https = require('https');
var str = '';
var url = "https://neenahrockets.nationbuilder.com/api/v1/people/count?access_token=**************";

exports.handler = function (event, context) {
    callback = function(response) {
      response.on('data', function (chunk) {
        str += chunk;
      });
      response.on('end', function () {
        console.log(req.data);
        console.log(str);
      });
    }

    var options = {
        url : url,
        method: "GET",
        json: true,
        headers: {
            "content-type": "application/json",
        },
    }

    var req = https.get(options, callback)

};

Upvotes: 2

Views: 18079

Answers (1)

Peter Breen
Peter Breen

Reputation: 447

Thanks to Mark B for catching the problem with the URL attribute. Here's the code that's working for me. I made one other small change adding to the http request headers.

var package = require('./package.json');
var myNewApi = require('./lib/my_new_api.js');
var http = require("http");
var https = require("https");    

var str = '';

exports.handler = function (event, context) {
    callback = function(response) {    

      response.on('data', function (chunk) {
        str += chunk;
      });

      response.on('end', function () {
        console.log(req.data);
        console.log(str);

      });
    }

    var options = {
        host : 'neenahrockets.nationbuilder.com',
        path:  '/api/v1/people/count?access_token=*********',
        json: true,
        headers: {
            "content-type": "application/json",
            "accept": "application/json"
        },
    }

 var req = https.get(options, callback)

};

Upvotes: 2

Related Questions