Google App Script: URLFetchApp.fetch with header

26,744

Solution 1

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);

Solution 2

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.

Share:
26,744
user5868253
Author by

user5868253

Updated on July 09, 2022

Comments

  • user5868253
    user5868253 almost 2 years

    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

  • Chris
    Chris over 4 years
    That's the right way to set the method and headers but the crossDomain and async params you included in the options will do nothing. According to Google's docs those aren't support options: developers.google.com/apps-script/reference/url-fetch/… (my guess is they're jQuery options though....adding to the confusion)
  • PotatoSoop
    PotatoSoop about 3 years
    Thank you for this, didn't realize headers needed to be in their own object inside of the options object. Also, the Google documentation is not very helpfull