javascript - Node.js HTTP Get Request to Mailchimp Returns Error -


whenever run following curl code, request mailchimp 3.0 api goes through fine:

curl --request \ --url 'https://us12.api.mailchimp.com/3.0/' \ --user 'anystring:apikey' 

however, whenever make request api using node.js, receive following error:

got error: connect econnrefused 127.0.0.1:80 

i'm assuming i'm missing or have mismatched within .js file, ideas might be? node code below:

 "use strict"; /* globals require, console */ var http = require('http');   var options = {     url: 'https://us12.api.mailchimp.com/3.0/',     headers: {         'authorization': 'anystring:apikey',         'content-type': 'application/json',     } };  http.get(options, (res) => {   console.log(`got response: ${res.statuscode}`);   // consume response body   res.resume(); }).on('error', (e) => {   console.log(`got error: ${e.message}`); }); 

edit: using uncaught exception returns following:

error: connect econnrefused 127.0.0.1:80     @ object.exports._errnoexception (util.js:856:11)     @ exports._exceptionwithhostport (util.js:879:20)     @ tcpconnectwrap.afterconnect [as oncomplete] (net.js:1053:14) 

edit 2: fixed part of it. using url 1 of options instead of host. here correct code portion:

var options = {     host: 'https://us12.api.mailchimp.com/3.0/',     headers: {         'authorization': 'anystring:apikey',         'content-type': 'application/json',     } }; 

now i'm receiving traceback got response: 400 instead of data i'm looking pull.

according documentation there no url property in options. supposed specify host, , path.

your options object should follows.

var options = { host: 'https://us12.api.mailchimp.com', path : '/3.0/' headers: {     'authorization': 'anystring:apikey',     'content-type': 'application/json', }; 

Comments