Reputation: 45
I tried to run this function in excel online and seem to get an error on fetch. I've tried other variations and at times I get no errors but no response text. I'm new to typescript but I've successfully done the same/similar GET requests in Python with the requests library and also in VBA. Just would be nice to have this done in excel online. The headers for userid and pwd are because of anonymous authentication is required.
async function main(workbook: ExcelScript.Workbook) {
const myHeaders = new Headers({
'Content-Type': 'application/x-www-form-urlencoded',
'userid': 'UserID',
'pwd': 'Password'
});
const myRequest = new Request('URL', {
method: 'GET',
headers: myHeaders
});
const response = await fetch(myRequest)
console.log(response.text)
}
I've also tried:
async function main(workbook: ExcelScript.Workbook) {
// Get the active worksheet.
let sheet = workbook.getActiveWorksheet();
// Display the current worksheet's name.
console.log(sheet.getName());
const myHeaders = new Headers({
'Content-Type': 'application/x-www-form-urlencoded',
'userid': 'UserID',
'pwd': 'Password'
});
const myRequest = new Request('URL', {
method: 'GET',
headers: myHeaders
});
fetch(myRequest)
.then(function(response) {
return response.text();
}).then(function(text) {
console.log(text);
});
}
Upvotes: 0
Views: 2370
Reputation: 2478
Looks right to me. Just to keep things simple - you could try this. It directly includes header and adds try..catch to show the error.
try {
const response = await fetch('url', {
method: 'GET',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'userid': 'UserID',
'pwd': 'Password'
}
});
} catch (e) {
console.error('Error:', e);
}
Another aspect could be related to CORS requirements. You may also want to check the that. If the server you are requesting from doesn't support CORS, you should get an error in the console indicating that the cross-origin request is blocked due to the CORS Access-Control-Allow-Origin header being missing.
Upvotes: 2