David Jones
David Jones

Reputation: 10219

Socket hang up error from GitHub API using Node HTTP request

I'm making the following request:

var options = {
    host: "api.github.com",
    port: 443,
    path: "/repos/myusername/myreponame/issues",
    headers: {Authorization: "Basic " + new Buffer(username + ":" + password).toString("base64")}
};

var request = http.get(options, function(response) {
    response.on("data", function(data) {
        console.log("data");
    });
});

request.on("error", function(error) {
  console.log(error);
});

This results in an error:

{ [Error: socket hang up] code: 'ECONNRESET' }

Any ideas why?

Upvotes: 3

Views: 923

Answers (1)

David Jones
David Jones

Reputation: 10219

I figured this one out. I was using http instead of https. Here's the working code:

var https = require("https");

username = "myusername";
password = "mypassword";

var options = {
    host: "api.github.com",
    port: 443,
    path: "/repos/myusername/myreponame/issues",
    headers: {
        "Authorization": "Basic " + new Buffer(username + ":" + password).toString("base64"),
        "User-Agent": username
    }
};

var request = https.get(options, function(response) {
    response.on("data", function(data) {
        process.stdout.write(data);
    });
});

request.on("error", function(error) {
  console.log(error);
});

Upvotes: 4

Related Questions