I have a very simple nodejs program that I am using to demonstrate communication with the Alchemer API. Whenever I run the following code, an error is thrown with the code ECONNREFUSED
. However, when I copy the same url into the browser or Postman, it works just fine.
Note- One odd thing I noticed, in the error's stack-trace (see below), it says the address is 127.0.0.1, even though I'm clearly not making a request to my localhost...
require('dotenv').config();
const https = require('https');
const querystring = require('querystring');
console.log('Formulating query string...');
const params = {
api_token: process.env.API_TOKEN,
api_token_secret: process.env.API_TOKEN_SECRET
};
const reqArgs = querystring.stringify(params);
console.log('Query string: ', reqArgs);
const reqOptions = {
url: 'https://api.alchemer.com',
path: '/v5/survey?' + reqArgs,
}
console.log('Test Url: ', reqOptions.url + reqOptions.path); // THIS WORKS IN THE
BROWSER / POSTMAN
console.log('Getting data...');
https.request(reqOptions, resp => {
let data = '';
resp.on('data', chunk => {
data += chunk;
});
resp.on('error', err => {
console.error(JSON.stringify(err));
});
resp.on('end', () => {
console.log('Request complete.');
console.log(data);
});
});
events.js:292
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:443
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
Emitted 'error' event on ClientRequest instance at:
at TLSSocket.socketErrorListener (_http_client.js:469:9)
at TLSSocket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 443
}
URL does not appear to be the correct property name for setting your URL in the request options, according to the https.requests doc here.
Consider changing the url
key on your reqOptions
to hostname
or host
const reqOptions = {
hostname: 'https://api.alchemer.com',
path: '/v5/survey?' + reqArgs,
}