每当我运行以下curl代码时,我对Mailchimp 3.0 API的请求就会很好:
curl --request GET \ --url 'https://us12.api.mailchimp.com/3.0/' \ --user 'anystring:APIKEY'但是,每当我使用Node.js向API发出请求时,都会收到以下错误:
Got error: connect ECONNREFUSED 127.0.0.1:80我假设我错过了某些内容或者在我的.js文件中有不匹配的内容,有什么想法可能是什么? 节点代码如下:
"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}`); });编辑:使用未捕获的异常返回以下内容:
Error: connect ECONNREFUSED 127.0.0.1:80 at Object.exports._errnoException (util.js:856:11) at exports._exceptionWithHostPort (util.js:879:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1053:14)编辑2:修复了它的一部分。 使用url作为我的选项之一而不是host 。 以下是该部分的正确代码:
var options = { host: 'https://us12.api.mailchimp.com/3.0/', headers: { 'Authorization': 'anystring:APIKEY', 'Content-Type': 'application/json', } };现在我收到了回溯Got response: 400而不是我想要拉的数据。
Whenever I run the following curl code, my request to the Mailchimp 3.0 API goes through just fine:
curl --request GET \ --url 'https://us12.api.mailchimp.com/3.0/' \ --user 'anystring:APIKEY'However, whenever I make a request to the API using Node.js, I receive the following error:
Got error: connect ECONNREFUSED 127.0.0.1:80I'm assuming I'm missing something or have something mismatched within my .js file, any ideas as to what that might be? Node code is 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 the following:
Error: connect ECONNREFUSED 127.0.0.1:80 at Object.exports._errnoException (util.js:856:11) at exports._exceptionWithHostPort (util.js:879:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1053:14)EDIT 2: Fixed part of it. Was using url as one of my options instead of host. Here is the correct code for that portion:
var options = { host: 'https://us12.api.mailchimp.com/3.0/', headers: { 'Authorization': 'anystring:APIKEY', 'Content-Type': 'application/json', } };Now I'm receiving the traceback Got response: 400 instead of the data I'm looking to pull.
最满意答案
根据文档 ,选项中没有url属性。 您应该指定主机和路径。
您的选项对象应如下所示。
var options = { host: 'https://us12.api.mailchimp.com', path : '/3.0/' headers: { 'Authorization': 'anystring:APIKEY', 'Content-Type': 'application/json', };Alright I seem to have it working, a few things needed to be changed:
As @I-Am-Not-Legend mentioned, the host and path options both needed to be set correctly. The anystring string needed to be changed to apikeyOnce I did these two things, the request worked as expected. Full code is below, just make sure to switch out APIKEY with your actual API key, and switch out us12 with your account's corresponding datacenter (found at the end of your API key).
"use strict"; /* globals require, console */ var http = require('http'); var options = { host: 'us12.api.mailchimp.com', path: '/3.0/', headers: { 'Authorization': 'apikey APIKEY', 'Content-Type': 'application/json', } }; http.get(options, (res) => { console.log(`Got response: ${res.statusCode}`); res.on('data', (chunk) => { console.log(`BODY: ${chunk}`); }); // consume response body res.resume(); }).on('uncaughtException', function (err) { console.log(err); });更多推荐
发布评论