1

I am trying to automate the addition of subscribers to a mailchimp mailing list using a google doc and google app script, which connects to the external mailchimp API. Whenever I try to test my script I receive a 'bad value' exception from StackDriver Log.

var payload = {
    "email_address": email,
    "status_if_new": "subscribed",
    "merge_fields": {
      FNAME: first_name,
      LNAME: last_name
    },
  };

  var headers = {
    "contentType": "application/json",
    "Authorization": "apikey " + API_KEY,
  };

  var data = {
    "method": "PUT",
    "muteHttpExceptions": false,
    "headers": headers,
    "payload": payload,
  };

  var json = Utilities.jsonStringify(data);
  
  //DEBUG
  Logger.log(json);
  Logger.log(endpoint);

  // call the Mailchimp API
  try {
    var response = UrlFetchApp.fetch(endpoint, json);

    if (response.getResponseCode() === 200) {
      // all good!
      Logger.log("Success");
      Logger.log(response);
    } else {
      Logger.log("Issues");
      Logger.log(response);
    }
  } catch (err) {
    // handle the error here
    Logger.log("Error");
    Logger.log(err);
  }
Sep 5, 2020, 1:02:53 PM Info Exception: Bad value

Upon further investigation with curl, I found that manually sending a request with the same payload and headers gave this response.

curl -X PUT 'https://{server}.api.mailchimp.com/3.0/lists/{my_list}/members/{member_hash}' 
         -H {"Authorization":"apikey {API_KEY}","contentType":"application/json"} 
         -d '{"email_address":"test@test.com","status_if_new":"subscribed","merge_fields":{"LNAME":"test","FNAME":"test"}},"muteHttpExceptions":false}' 

"errors":[{"field":"email_address","message":"This value should not be blank."}]

Why does the API not recognize the email I am providing?

1 Answers1

2

The main issue is that you're passing a JSON string as the params argument where an object is expected in UrlFetchApp.fetch(url, params). Instead of stringifying all of data, you only need to stringify the payload. (Note the example linked in the Apps Script documentation.)

I suspect you'll also face an issue with Utilities.jsonStringify() as it's deprecated. Use JSON.stringify() instead.

I also moved contentType out of headers and into data, as this is how it's specified in the fetch() method, but it actually has no impact on your application.

var payload = {
  "email_address": email,
  "status_if_new": "subscribed",
  "merge_fields": {
    FNAME: first_name,
    LNAME: last_name
  },
};

var headers = {
  "Authorization": "apiKey " + API_KEY
};

var data = {
  "method": "PUT",
  "muteHttpExceptions": false,
  "headers": headers,
  "contentType": "application/json",
  "payload": JSON.stringify(payload) // The payload should be stringified
};

//DEBUG
Logger.log(endpoint);

// call the Mailchimp API
try {
  var response = UrlFetchApp.fetch(endpoint, data); // pass in data, not json

  if (response.getResponseCode() === 200) {
    // all good!
    Logger.log("Success");
    Logger.log(response);
  } else {
    Logger.log("Issues");
    Logger.log(response);
  }
} catch (err) {
  // handle the error here
  Logger.log("Error");
  Logger.log(err);
}

If you're only going to be adding subscribers, then I'd recommend you use the add members to list endpoint. It's just a bit more straightforward and doesn't require that you generate a member hash.

Diego
  • 9,261
  • 2
  • 19
  • 33