简体   繁体   中英

HTTPS GET API call in node.js

Im new to node.js and consuming API's. Im following this tutorial for how to make a GET request but I keep getting this error:

Error: connect ECONNREFUSED 127.0.0.1:800 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) { errno: 'ECONNREFUSED', code: 'ECONNREFUSED', syscall: 'connect',
address: '127.0.0.1', port: 800 }

I'm not sure what to do now, any feedback is appreciated!

My 'DistributionList.js' file

const https = require("https");

const options = {
    method: 'GET',
    url: 'https://SomeApi.com/distributionLists',
    headers: {
        Authorization: "Bearer xyz",
        Accept: "application/json"
    }
}

const req = https.request(options, res => {
    console.log(`statusCode: ${res.statusCode}`)

    res.on('data', d => {
      process.stdout.write(d)
    })
})

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

req.end()

Removing the URL and replacing it with a host and path worked for me!

const https = require("https");

const options = {
    method: 'GET',
    host : 'SomeApi.com',
    path:  '/distributionLists',
    headers: {
        Authorization: "Bearer xyz",
        Accept: "application/json"
    }
}

const req = https.request(options, res => {
    console.log(`statusCode: ${res.statusCode}`)

    res.on('data', d => {
      process.stdout.write(d)
    })
})

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

req.end()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM