Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google App Script: URLFetchApp.fetch with header

I'm trying to fetch data from the rainforestqa API but to gain access I need to send my api_key as a header. The code I already have is as follows,

var header = {
     "access-control-allow-headers":"Content-Type",
     "CLIENT_TOKEN" : "API-TOKEN"
}; 

var options = {
     "method" : "post",
     "header" : header
};

UrlFetchApp.fetch("https://app.rainforestqa.com:443/api/1/runs/TESTNUMBER/tests.json?result=failed", options);

But this returns 405 error. Does anyone have any ideas why this isn't working?

Thanks

like image 720
user5868253 Avatar asked Feb 01 '16 14:02

user5868253


2 Answers

It turns out the answer is as follows, I basically got to this via trial and error.

$var options = {
     "async": true,
     "crossDomain": true,
     "method" : "GET",
     "headers" : {
       "CLIENT_TOKEN" : "my-api-key",
       "cache-control": "no-cache"
     }
   };
var response = UrlFetchApp.fetch("https://app.rainforestqa.com:443/api/1/runs/test_id/tests.json?result=failed", options);
like image 195
user5868253 Avatar answered Nov 15 '22 21:11

user5868253


I believe you have misspelled the word headers. I know it is an old question, but I did not see this answer before and it might help others who arrive this page.

like image 36
jgggr Avatar answered Nov 15 '22 19:11

jgggr