michael
michael

Reputation: 197

JS Fetch() Get Header Meta Information via Variable or Function

I have multiple functions, that call fetch():

function FetchData(...) {
   (async () => {
      ...
      var res = await fetch(url, {
         method: 'POST',
         headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
         },
         body: JSON.stringify({...})
      });
      ...
   })();
}

I would like get the header via a Global Variable (or better a function which returns the value), but this is not working yet.

Goal is:

function FetchData(...) {
   (async () => {
      ...
      var res = await fetch(url, {
         method: 'POST',
         ReturnHeader(),
         body: JSON.stringify({...})
      });
      ...
   })();
}

Upvotes: 0

Views: 191

Answers (1)

Barmar
Barmar

Reputation: 782775

Call the function in the value of the property.

function FetchData(...) {
   (async () => {
      ...
      var res = await fetch(url, {
         method: 'POST',
         headers: ReturnHeader(),
         body: JSON.stringify({...})
      });
      ...
   })();
}

Upvotes: 1

Related Questions