'displayDialogAsync - how to include request header to a URL?

I have a site that expects a custom request header like this:

GET https://example.com
AuthToken: mYCustomT0k3nHere

How do I send such AuthToken header when opening this site from OfficeJS ?

function openMySite() {
     var authToken = "mYCustomT0k3nHere";
     //how to send authToken when requesting mysite.com ?
     Office.context.ui.displayDialogAsync('https://example.com');
}


Solution 1:[1]

There is no way to customize the Request object for the URL that you pass to displayDialogAsync. A possible workaround is to have the page that you pass to displayDialogAsync run JavaScript that makes an ajax call with a custom header. Search for "how to attach custom request header with javascript".

Solution 2:[2]

This is how I use fetch with headers:

let token = "mYCustomT0k3nHere";
let url = 'https://example.com';
let url_obj = {
  validateHttpsCertificates: true,
  method: 'GET',
  contentType: 'application/json',
  headers: {
    'Authorization': 'Bearer ' + token
  },
  muteHttpExceptions: true
}
let response = await fetch(url, url_obj);
if (!response.ok) {
  // throw error.
  let err = new Error(response.statusText);
  throw err;
}
// try to parse and return the content of http response.
let resp = await response.json();
return resp;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rick Kirkham
Solution 2 Ninca Tirtil