更新:我已经用推荐的答案更新了我的代码,现在得到的错误与我最初的错误不同,如下所述。
我正在使用 Meteor js http 包,我正在尝试向Constant Contact API发送 POST 请求。我正在尝试使用该data
选项来传递一个支持 JSON 的对象来字符串化并用作 HTTP 请求正文。我收到来自 Constant Contact 的 400 错误响应。使用Constant Contact API 测试器,我能够成功获得 201 响应并添加联系人。我在这里拥有的 Json 与我在测试器中使用的 Json 相同,但我得到了以下错误。
{ [Error: failed [400] [{"error_key":"query.param.invalid","error_message":"The query parameter status is not supported."},{"error_key":"query.param.invalid","error_message":"The query parameter limit is not supported."}]]
下面是我的代码。
var data = {
"addresses": [
{
"address_type": "BUSINESS",
"city": "Belleville",
"country_code": "CA",
"line1": "47 Shawmut Ave.",
"line2": "Suite 404",
"postal_code": "K8b 5W6",
"state_code": "ON"
}
],
"lists": [
{
"id": "1395617465"
}
],
"cell_phone": "555-555-5555",
"company_name": "System Optimzations",
"confirmed": false,
"email_addresses": [
{
"email_address": "username2@example.com"
}
],
"fax": "555-555-5555",
"first_name": "Ronald",
"home_phone": "555-555-5555",
"job_title": "Systems Analyst 3",
"last_name": "Martone",
"prefix_name": "Mr.",
"work_phone": "555-555-5555"
};
HTTP.post('https://api.constantcontact.com/v2/contacts?status=ALL&limit=50&api_key=<random-key>', {
headers: {
'Authorization': 'Bearer <random-token>',
'Content-Type': 'application/json'
},
data: JSON.stringify(data)
}, function (error, response) {
if ( error ) {
console.log( error );
} else {
console.log(response);
}
});