I have generated an Office Add-In for Excel using the Yo Office Generator.
From that add-in, I would like to call a RESTful API.
GET requests work as expected using fetch, but I am trying to implement the batched query pattern provided by Microsoft. I expect the query string to be too long to use the GET method for a typical batch. I want to use the POST method so that I can have many different objects in the API request body.
My actual use case (simplified here) is to return results for a batch of calculations, performed on the server.
I have the following custom function, posting to an online test API:
/**
* Test Post Method
* @customfunction
* @returns {string}
*/
async function PostTest() {
try {
const url = 'https://reqbin.com/sample/post/json';
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({'key':'value'})
});
if (!response.ok) {
throw new Error(response.statusText)
}
const jsonResponse = await response.json();
return jsonResponse;
}
catch (error) {
return error.name + ' ' + error.message
}
}
The fetch call fails with:
TypeError Network request failed
I have read the issues with CORS mentioned on this site, especially this one. I think that CORS wouldn't be an issue with the resource that I am accessing. I have enabled shared run time. I need this to work on Excel Desktop client.
Thank you in advance.