3

UPDATE: I have updated my code with the recommended answer, and am now getting a different error than I originally did, explained below.

I am using Meteor js http package and I am attempting to send a POST request to the Constant Contact API. I am trying to use the data option to pass in a JSON-able object to stringify and use as the HTTP request body. I am getting a 400 error response from Constant Contact. Using the Constant Contact API tester I was able to successfully get a 201 response and Add a contact. The Json I have here is the same that I used in the tester but I get the following error back.

{ [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."}]]

Here is my code below.

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

      }
    });
Anders Kitson
  • 1,413
  • 6
  • 38
  • 98
  • headers: { 'Content-Type': 'application/json; charset=utf-8' } – orangespark Oct 06 '17 at 06:15
  • @orangespark that doesn't seem to make a difference. – Anders Kitson Oct 06 '17 at 17:39
  • @AndersKitson Are you sure you updated your code based the recommended answer? I can still see your endpoint is incorrect. And so does the error reflects the same. – timekeeper Oct 08 '17 at 04:11
  • I changed my endpoint and I now gett this error `{ [Error: failed [400] [{"error_key":"json.type.invalid","error_message":"#/: Value is of a disallowed type. Allowed types are: Object."}]]` – Anders Kitson Oct 08 '17 at 15:23

2 Answers2

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

Now , convert object to JSON using JSON.stringify and add Content-Type header.

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

          }
        });
Atul Sharma
  • 9,397
  • 10
  • 38
  • 65
  • I am now getting a error `Unexpected token, expected ,` on the last line, something must be wrong. – Anders Kitson Oct 01 '17 at 16:36
  • I have updated my question with the code fixed for the error, now I am trying to figure out why the response from Constant Contact api is giving me a 400, I have updated my question and code above. – Anders Kitson Oct 03 '17 at 14:58
2

The correct url for POST is
https://api.constantcontact.com/v2/contacts?action_by=ACTION_BY_OWNER&api_key=<api-key>

not

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

Please see docs here: https://constantcontact.mashery.com/io-docs Contact methods section.

Kosh
  • 16,966
  • 2
  • 19
  • 34
  • I still get the same 400 response error when I change the url. – Anders Kitson Oct 07 '17 at 20:44
  • @AndersKitson, If you get exactly the same error, you probably have not changed the url (or have changed it in the wrong place, or haven't saved the changes etc.). If the error text is different, please post it. – Kosh Oct 08 '17 at 05:17
  • this is the new error I am getting `{ [Error: failed [400] [{"error_key":"json.type.invalid","error_message":"#/: Value is of a disallowed type. Allowed types are: Object."}]]` – Anders Kitson Oct 08 '17 at 14:25