google-apps-scriptgoogle-apps-script-api

Google Apps Script PUT requests failing


I'm having issues with calling the Streak API with Google Apps Script. Any of the GET or POST requests I make are working, but I can't get a single PUT request to work.

Since the matching CURL requests work I'm thinking it's something I'm doing wrong with the GAS request? Any help would be appreciated!

For example this:

  var streakApiKey = "<my-api-key>";
  var streakBoxKey = "agxzfm1haWxmb29nYWVyLwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEgRDYXNlGKGs7kkM";
  var gmailThreadId = "1611ad242bc28086";
  var url = "https://www.streak.com/api/v1/boxes/" + streakBoxKey + "/threads/";

  var payload = {
    "boxKey": streakBoxKey,
    "threadGmailId": gmailThreadId
    };

  var headers = {
     "Accept": "application/json",
     "Content-Type": "application/json",
     "Authorization": "Basic "+ Utilities.base64Encode(streakApiKey + ":")
     };

  var options = {
    "method": "put",
    "headers": headers,
    "contentType" : "application/json",
    "payload": JSON.stringify(payload),
    "muteHttpExceptions" : true,
    };

  var request = UrlFetchApp.getRequest(url, options);
  Logger.log(request);

  var response = UrlFetchApp.fetch(url,options);
  Logger.log(response.getContentText());

Returns:

[18-02-04 10:58:30:284 NZDT] {headers={Authorization=Basic <my-encoded-api-key>, Accept=application/json}, method=put, payload={"boxKey":"agxzfm1haWxmb29nYWVyLwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEgRDYXNlGKGs7kkM","threadGmailId":"1611ad242bc28086"}, followRedirects=true, validateHttpsCertificates=true, useIntranet=false, contentType=application/json, url=https://www.streak.com/api/v1/boxes/agxzfm1haWxmb29nYWVyLwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEgRDYXNlGKGs7kkM/threads/}
[18-02-04 10:58:30:311 NZDT] {
  "success": false,
  "error": "Insufficient params for GmailThread. Missing json"
}

But this CURL works:

 curl --request PUT \  --url https://www.streak.com/api/v1/boxes/agxzfm1haWxmb29nYWVyLwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEgRDYXNlGKGs7kkM/threads \  --data 'boxKey=agxzfm1haWxmb29nYWVyLwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEgRDYXNlGKGs7kkM&threadGmailId=1611ad242bc28086' -u <my-api-key>:

Similarly this fails:

var streakApiKey = "<my-api-key>";
  var pipelineKey = "agxzfm1haWxmb29nYWVyNwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEghXb3JrZmxvdxiAgICA1uaPCgw";
  var url = "https://www.streak.com/api/v1/pipelines/" + pipelineKey + "/stages";

  var payload = {
    "name": "new Stage from API",
    };

  var headers = {
     "Accept": "application/json",
     "Content-Type": "application/json",
     "Authorization": "Basic "+ Utilities.base64Encode(streakApiKey + ":")
     };

  var options = {
    "method": "put",
    "headers": headers,
    "contentType" : "application/json",
    "payload": JSON.stringify(payload),
    "muteHttpExceptions" : true,
    };

  var request = UrlFetchApp.getRequest(url, options);
  Logger.log(request);

  var response = UrlFetchApp.fetch(url,options);
  Logger.log(response.getContentText());

and returns:

[18-02-04 11:02:01:600 NZDT] {headers={Authorization=Basic <my-encoded-api-key>, Accept=application/json}, method=put, payload={"name":"new Stage from API"}, followRedirects=true, validateHttpsCertificates=true, useIntranet=false, contentType=application/json, url=https://www.streak.com/api/v1/pipelines/agxzfm1haWxmb29nYWVyNwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEghXb3JrZmxvdxiAgICA1uaPCgw/stages}
[18-02-04 11:02:01:632 NZDT] {
  "success": false,
  "error": "Insufficient params for Stage"
}

Solution

  • How about this modification?

    Modification points :

    The modified scripts which reflected above points are as follows.

    For sample 1 :

    var streakApiKey = "<my-api-key>";
    var streakBoxKey = "agxzfm1haWxmb29nYWVyLwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEgRDYXNlGKGs7kkM";
    var gmailThreadId = "1611ad242bc28086";
    var url = "https://www.streak.com/api/v1/boxes/" + streakBoxKey + "/threads/";
    var payload = {
      "boxKey": streakBoxKey,
      "threadGmailId": gmailThreadId
    };
    var headers = {
    //  "Accept": "application/json", // Modified (I couldn't confirm whether this is required.)
    //  "Content-Type": "application/json", // Modified
      "Authorization": "Basic "+ Utilities.base64Encode(streakApiKey + ":")
    };
    var options = {
      "method": "put",
      "headers": headers,
    //  "contentType" : "application/json", // Modified
      "payload": payload, // Modified
      "muteHttpExceptions" : true,
    };
    var request = UrlFetchApp.getRequest(url, options);
    Logger.log(request);
    var response = UrlFetchApp.fetch(url,options);
    Logger.log(response.getContentText());
    

    For sample 2 :

    var streakApiKey = "<my-api-key>";
    var pipelineKey = "agxzfm1haWxmb29nYWVyNwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEghXb3JrZmxvdxiAgICA1uaPCgw";
    var url = "https://www.streak.com/api/v1/pipelines/" + pipelineKey + "/stages";
    var payload = {
      "name": "new Stage from API",
    };
    var headers = {
    //  "Accept": "application/json", // Modified (I couldn't confirm whether this is required.)
    //  "Content-Type": "application/json", // Modified
      "Authorization": "Basic "+ Utilities.base64Encode(streakApiKey + ":")
    };
    var options = {
      "method": "put",
      "headers": headers,
    //  "contentType" : "application/json", // Modified
      "payload": payload, // Modified
      "muteHttpExceptions" : true,
    };
    var request = UrlFetchApp.getRequest(url, options);
    Logger.log(request);
    var response = UrlFetchApp.fetch(url,options);
    Logger.log(response.getContentText());
    

    Note :

    If this didn't work, I'm sorry.