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"
}
How about this modification?
JSON.stringify()
is used for "payload" withoud "application/json", "form-data" is sent as a string.The modified scripts which reflected above points are as follows.
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());
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());
If this didn't work, I'm sorry.