Reputation: 13
I am trying to invoke an apps script deployed web app (doPost method) and get a response back to the client app (this could be HTTP client like Postman/Bruno or simple javascript app). The deployed web app would call an external API and return its result. When I try this I am getting a message (HTML) saying "The script completed but the returned value is not a supported return type." If I do not call external API and try to return a default / hard coded object from doPost, the client is getting the response properly (as a JSON). Console log statements are showing proper output values Please suggest if there is a way out.
Deployed web app (apps script) is
let respout= { "jokeid": 0, "errcode": 0, "errmsg": "na" };
function doPost()
{
console.log('inside doPost func', respout);
try
{ //If tried to return above respout with out executing "jokefun()", below statement works with client
//return ContentService.createTextOutput(JSON.stringify(respout)).setMimeType(ContentService.MimeType.JSON);
return jokefunc().then( res => {
respout={ "jokeid": res.id,"errcode": 200,"errmsg": "success" };
console.log('resp out val in then func',respout);
return ContentService.createTextOutput(JSON.stringify(respout)).setMimeType(ContentService.MimeType.JSON);
})
}
catch (error)
{
console.log('Exception in doPost', error);
}
}
async function jokefunc()
{
try
{
const API_URL = "https://official-joke-api.appspot.com/random_joke";
const response = UrlFetchApp.fetch(API_URL);
const responseText = response.getContentText();
const jsonData = JSON.parse(responseText);
console.log('Fetch called', jsonData);
return jsonData; // Return the fetched data object
}
catch(error)
{
console.error('Some thing went wrong',error)
}
}
Upvotes: 0
Views: 152
Reputation: 13
I just tried by removing the async and called in a synchronous way in the doPost and it worked like a charm.
Upvotes: 1