18

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

5
  • Since 405 is "Method not allowed", I assume the server doesn't allow a POST method on the resource you're trying to access. Have you tried a GET? Commented Feb 1, 2016 at 15:05
  • Hi, thanks for the comment. When I change this to GET I get a 401 which presumabley means that the header isn't being passed to the server and the authentication isn't working. Commented Feb 1, 2016 at 16:02
  • I've just taken a very quick glance at Rainforest and I see access-control-allow-headers:Content-Type,CLIENT_TOKEN, which suggests to me that the API key header should be called CLIENT_TOKEN. What happens if you change "api_key" to "CLIENT_TOKEN"? Commented Feb 1, 2016 at 16:09
  • Hi thanks again for your help. I've updated my question to show the changes you suggested. However, this still gives a 401. I'm starting to think this might be something to do with rainforestqa's API... Commented Feb 1, 2016 at 16:39
  • 1
    Note clients should never send access-control-allow-headers -- that is for servers to send to clients. However, since you're getting a 401 response, that issue not what is causing your problem; it's simply needless code (but it would be problem if you tried it in a browser context). Commented Feb 1, 2016 at 19:23

2 Answers 2

42

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.