【问题标题】:UPDATE: How to send Json Data as a POST request in Meteor using http package更新:如何使用 http 包在 Meteor 中将 Json 数据作为 POST 请求发送
【发布时间】:2017-09-29 11:54:19
【问题描述】:

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

我正在使用 Meteor js http package 并尝试向 Constant Contact API 发送 POST 请求。我正在尝试使用 data 选项传入一个支持 JSON 的对象以进行字符串化并用作 HTTP 请求正文。我收到来自 Constant Contact 的 400 错误响应。使用 Constant Contact API tester 我能够成功获得 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);

      }
    });

【问题讨论】:

  • headers: { 'Content-Type': 'application/json;字符集=utf-8' }
  • @orangespark 似乎没有什么不同。
  • @AndersKitson 你确定你根据推荐的答案更新了你的代码吗?我仍然可以看到您的端点不正确。错误也反映了同样的情况。
  • 我改变了我的端点,现在我得到了这个错误{ [Error: failed [400] [{"error_key":"json.type.invalid","error_message":"#/: Value is of a disallowed type. Allowed types are: Object."}]]

标签: javascript json rest post constantcontact


【解决方案1】:

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

不是

https://api.constantcontact.com/v2/contacts?status=ALL&amp;limit=50&amp;api_key=&lt;random-key&gt;

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

【讨论】:

  • 我在更改 url 时仍然收到相同的 400 响应错误。
  • @AndersKitson,如果你得到完全相同的错误,你可能没有更改 url(或者在错误的地方更改了它,或者没有保存更改等)。如果错误文本不同,请发布。
  • 这是我收到的新错误{ [Error: failed [400] [{"error_key":"json.type.invalid","error_message":"#/: Value is of a disallowed type. Allowed types are: Object."}]]
【解决方案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);

          }
        });

【讨论】:

  • 我现在在最后一行收到一个错误Unexpected token, expected ,,一定有问题。
  • 我已经用为错误修复的代码更新了我的问题,现在我试图弄清楚为什么来自 Constant Contact api 的响应给我一个 400,我已经更新了我的问题和上面的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-27
  • 1970-01-01
  • 2016-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-26
相关资源
最近更新 更多