[SalesForce] SSJS – HTTP GET for API

I am trying to return an array of Event objects using SSJS HTTP GET with an authorization bearer header. This is my current script but it isn't loading anything. Would anyone be able to assist? I am very new to SSJS. Thanks!

<script runat="server">
Platform.Load("Core","1");
var apitoken = Variable.GetValue("@apitoken");
var url = "https://xxxxx.com.au/api/v1/events";
var headerNames = ("Authorization");
var headerValues = ("Bearer", apitoken);
var response = HTTP.Get(url, headerNames, headerValues);
Variable.SetValue("response",response.Content);

</script>

Best Answer

Your header names and values need to be an array. See below example as per documentation

var url = 'http://www.example.com';
var headerNames = ["MyTestHeader1", "MyTestHeader2"];
var headerValues = ["MyTestValue1", "MyTestValue2"];
var response = HTTP.Get(url, headerNames, headerValues);

Also note Lucas' comment re space between bearer and token. In addition to this, to concatenate strings in javascript use + as such:

var headerValues = ["Bearer " + apitoken];

The above joins the string "Bearer " with the value of the apiToken variable and sets as first item in an array (indicated by the square brackets) which is assigned to the variable headerValues.

Related Topic