简体   繁体   English

对Mailchimp的Node.js HTTP Get请求返回错误

[英]Node.js HTTP Get Request to Mailchimp Returns Error

Whenever I run the following curl code, my request to the Mailchimp 3.0 API goes through just fine: 每当我运行以下curl代码时,我对Mailchimp 3.0 API的请求都会顺利通过:

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: 但是,每当我使用Node.js向API请求时,都会出现以下错误:

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? 我假设我的.js文件中缺少某些内容或存在某些不匹配的内容,那么可能会有什么想法吗? 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. 编辑2:固定的一部分。 Was using url as one of my options instead of host . 使用url作为我的选择之一,而不是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. 现在,我收到了回溯Got response: 400而不是我要提取的数据。

According to the documentation there is no url property in options. 根据文档 ,选项中没有url属性。 You are supposed to specify a host, and a path. 您应该指定一个主机和一个路径。

Your options object should look as follows. 您的选项对象应如下所示。

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: 好吧,我似乎可以正常使用,需要做一些更改:

  1. As @I-Am-Not-Legend mentioned, the host and path options both needed to be set correctly. 如@ I-Am-Not-Legend所述,主机和路径选项都需要正确设置。
  2. The anystring string needed to be changed to apikey anystring字符串需要更改为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). 全部代码如下,只要确保转出APIKEY与您的实际API密钥,并转出us12与您帐户的相应数据中心(在你的API密钥的结尾处找到)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM