title: "JSON_PARSING_ERROR: Unexpected character (d) at position 0." when sending GCM request from GAS
body: <p>This seems very similar to a number of other questions and it seems obvious that the error indicates there's something wrong with my JSON payload.  But I'm at a loss as to why.</p>
  
  <p>I'm running a Google Apps Script to test sending a message to <strike>Google</strike> Firebase Cloud Messaging.</p>
  
  <p>My code:</p>
  
  <pre><code>function SendGCMessage() {
    var url = "https://gcm-http.googleapis.com/gcm/send";
    var apiKey = "AbCdEfG";
    var to = "ZyXwVuT:ToKeNtOkEnToKeNtOkEnToKeNtOkEn"
    var payload = {
      "data": {
        "message" : "This is the message"
      },
      "to":to
    };
  
    var sendCount = 1;
      var headers = {
        "Content-Type": "application/json",
        "Authorization": "key=" + apiKey
      };
      var params = {
        headers: headers,
        method: "post",
        payload: payload
      };
  
      var response = UrlFetchApp.fetch(url, params);
    return {message: "send completed: " + response.getContentText()};
  }
  </code></pre>
  
  <p>When I run this in debug mode, the object payload looks fine - like a normal Javascript object.  params as well.  UrlFetchApp takes a Javascript object, not a String in JSON notation.  However I did try "JSON.stringify(params)" and I got an error. What did I do wrong?</p>
  
  <p>Note: params looks like this when I pause it in the debugger:</p>
  
  <blockquote>
    <p>{"headers":{"Content-Type":"application/json","Authorization":"key=AbCdEfG"},"method":"post","payload":{"data":{"message":"This
    is the message"},"to":"ZyXwVuT:ToKeNtOkEnToKeNtOkEnToKeNtOkEn"}}</p>
  </blockquote>