0

I have this string in a Google Apps Script

const response = UrlFetchApp.fetch(url, { 'method': 'POST', 'headers': {'Authorization': 'Basic QWRtaW5pc3RyYXRv','Content-Type': 'application/json'}}); I can't find a working way to have the string "QWRtaW5pc3RyYXRv" read from a variable.

Something like var key = "QWRtaW5pc3RyYXRv"; const response = UrlFetchApp.fetch(url, { 'method': 'POST', 'headers': {'Authorization': 'Basic + key +','Content-Type': 'application/json'}});

Does not work!!

  • Welcome to [Stack Overflow](https://stackoverflow.com/tour). See [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – doubleunary Oct 07 '22 at 15:08

1 Answers1

1

Use a template literal, like this:

  const key = 'QWRtaW5pc3RyYXRv';
  const auth = `Basic ${key}`;
  const headers = { 'Authorization': auth, 'Content-Type': 'application/json' };
  const response = UrlFetchApp.fetch(url, { 'method': 'POST', 'headers': headers });
doubleunary
  • 13,842
  • 3
  • 18
  • 51