簡體   English   中英

更新:如何使用http包在流星中將Json數據作為POST請求發送

[英]UPDATE: How to send Json Data as a POST request in Meteor using http package

更新 :我已經用推薦的答案更新了代碼,現在得到的錯誤與我最初的錯誤不同,下面將對此進行說明。

我正在使用Meteor js http程序包,並且試圖將POST請求發送到Constant Contact API 我正在嘗試使用data選項來傳遞可支持JSON的對象以進行字符串化並用作HTTP請求正文。 我從持續接觸中收到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);

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

          }
        });

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“ 聯系方法”部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM