Outbound Calling — HTTPS GET with Authorization

In this code sample, the NSOA.https.get function is used to get a protected resource. A Password script parameter is used to store the API token securely. The headers are created with the API token and then passed with the URL to the NSOA.https.get function.

          /**
 * Get a protected resource from a URL.
 * @param {Str}  url     API URL (required)
 * @return {Obj}        An https.get response object.
 */
function getProtectedResource(url) {
   // Check that url parameter has a value, otherwise return
  url = url || '';
  if (!url || url.length === 0) { return null; }
   
   // Retrieve API auth token from script parameter
   api_token = NSOA.context.getParameter('api_token');
​
  var headers = {
    'Authorization': 'Bearer ' + api_token
  };
​
  var response = NSOA.https.get({
    url: url,
    headers: headers
  });
​
  return response;
} 

        

See also: