2

更新:我已经用推荐的答案更新了我的代码,现在得到的错误与我最初的错误不同,如下所述。

我正在使用 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);

      }
    });
4

2 回答 2

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

JSON.stringify现在,使用并添加标头将对象转换为 JSON Content-Type

HTTP.post('https://api.constantcontact.com/v2/contacts?action_by=ACTION_BY_OWNER&api_key=<api-key>',{
          headers:{
            'Authorization': 'Bearer <api-key>',
            'Content-Type': 'application/json'
          },
          data: JSON.stringify(data),
        function (error, response) {
          if ( error ) {
            console.log( error );
          } else {

              console.log(response);

          }
        });
于 2017-10-01T15:07:21.697 回答
2

POST 的正确网址是
https://api.constantcontact.com/v2/contacts?action_by=ACTION_BY_OWNER&api_key=<api-key>

不是

https://api.constantcontact.com/v2/contacts?status=ALL&limit=50&api_key=<random-key>

请在此处查看文档:https ://constantcontact.mashery.com/io-docs 联系方法部分。

于 2017-10-07T19:51:13.787 回答