ryanve
ryanve

Reputation: 52531

Node.js Github API connect ECONNREFUSED error getting user repos

I'm locally attempting to get user repos via https get request with https.get

const https = require("https")

https.get({
    url: "https://api.github.com/users/ryanve/repos"
  }, response => {
    let data = ""
    response
      .on("data", chunk => data += chunk)
      .on("end", () => {
        console.log(data)
      })
  })
  .on("error", e => {
    console.error(e.message)
  })

I get an ECONNREFUSED error

connect ECONNREFUSED 127.0.0.1:443

What causes this error?

Authentication?

How do I fix?

node --version is v12.16.2

Upvotes: 2

Views: 1211

Answers (2)

ryanve
ryanve

Reputation: 52531

Steps to get it working

  1. Corrected the url syntax. Thanks!
  2. Got Please make sure your request has a User-Agent header error
  3. Tested that curl worked to same endpoint
  4. Learned how to send User-Agent in options.headers

Working curl

curl -iH User-Agent:ryanve https://api.github.com/users/ryanve/repos

Working https.get

https.get("https://api.github.com/users/ryanve/repos", {
    headers: {
      "User-Agent": "ryanve"
    }
  }, response => {
    let data = ""
    response
      .on("data", chunk => data += chunk)
      .on("end", () => {
        console.log(JSON.parse(data, null, 2))
      })
  })
  .on("error", e => {
    console.error(e.message)
  })

Upvotes: 1

You can't include url in an object that you pass to get. Since that's all you passed and it was ignored, it used a default URL of localhost. (IMO, having a default URL and not just throwing an error saying a URL is missing is a wart in NodeJS, but that's neither here nor there.) Replace this:

https.get({
    url: "https://api.github.com/users/ryanve/repos"
  }

With this:

https.get("https://api.github.com/users/ryanve/repos"

Upvotes: 1

Related Questions