1

I'm trying to send a GET request to a user/pass protected API. I'm using Javascript and adding in an "auth" option.

const https = require('https');
const headers= {
 'Accepts' : 'application/json',
}
const options = {
 hostname: 'api.com/more/info',
 port: 443,
 path: '/',
 auth: 'username:password',
 method: 'GET'
};
const req = https.request(options, (res) => {
 console.log('statusCode:', res.statusCode);
 console.log('headers:', res.headers);
 res.on('data', (d) => {
 process.stdout.write(d);
 });
});
req.on('error', (e) => {
 console.error(e);
});
req.end();

The error I receive when this code is run is:

Error: getaddrinfo ENOTFOUND api/com/more/info
 at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
 errno: -3008,
 code: 'ENOTFOUND',
 syscall: 'getaddrinfo',
 hostname: 'api.com/more/info'
}

The hostname has been changed for anonymity.

What am I doing wrong?

Thanks in advance for any advice.

asked May 9, 2020 at 17:21
1
  • Have you tried with Postman to send the request? Just to make sure that the DNS server which your computer pointing to can resolve api.com/more/info. Commented May 9, 2020 at 17:25

1 Answer 1

2

'api.com/more/info' is probably not your hostname.

Try with these options instead:

const options = {
 hostname: 'api.com',
 port: 443,
 path: '/more/info',
 auth: 'username:password',
 method: 'GET'
};
answered May 9, 2020 at 17:28
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.