Node.js HTTP获取Mailchimp请求返回错误(Node.js HTTP Get Request to Mailchimp Returns Error)

编程入门 行业动态 更新时间:2024-10-25 08:28:24
Node.js HTTP获取Mailchimp请求返回错误(Node.js HTTP Get Request to Mailchimp Returns Error)

每当我运行以下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:80

I'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 apikey

Once 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); });

更多推荐

本文发布于:2023-07-24 19:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250191.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   HTTP   Node   js   Mailchimp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!