I am trying to execute a simple apps script function deployed as web app. This web app has a single function doPost with a simple console print statement. When I try to execute this script from JS client using fetch method, I am getting error "Script function not found: doGet".
However when I execute this from a HTTP client tool like Bruno I am able to execute the web app and the results are as expected.
Please suggest how to resolve this issue.
Apps script code as below
function doPost(e) {
console.log('Posted data from client is..',JSON.stringify(e))
}
Client side JS Code as below
const url = "https://script.google.com/macros/s/myscriptid/exec";
let appbody = {
"fruit_name" : "Orange",
"qty" : "210"
}
let optionsin = {
"headers" : {
"Content-Type": "application/json"
},
"method": "POST",
"body": JSON.stringify(appbody)
}
function samplefunc()
{
try
{
console.log("About to call fetch")
fetch(url, {optionsin})
.then(response => {return response.json()} )
.then(result => {console.log(' result from api call', result);
})
}
catch (error)
{
console.log('Error in triggering fetch', error.message)
}
}
samplefunc();
2
Answers
The issue you’re facing with doPost not being called when using fetch but working with an HTTP client likely boils down to how the request is being sent. Here’s a breakdown of the problem and potential solutions:
The Problem:
Google Apps Script expects POST requests to have a specific format to trigger the doPost function.
fetch might not be adding the necessary headers or payload format that Apps Script recognizes as a POST request.
HTTP client tools like Bruno might be pre-configuring these details, causing them to work as expected.
Potential Solutions:
Modify fetch request:
Update the optionsin object in your client-side JS code to explicitly include the POST method and necessary headers:
JavaScript
If you’re using a framework like clasp to deploy your Apps Script as a web app, consider using the GAppsScript library. This library simplifies calling Apps Script functions from your client-side code and handles the request formatting automatically. Refer to the library’s documentation for specific usage instructions.
Even if doPost isn’t being triggered, there might be clues in the Apps Script logs. Go to Tools > Script editor in your Google Apps Script project. In the Script editor, navigate to Run > View logs to see if any errors or warnings are being logged related to your function call.
Modification points:
From your showing Javascript, I suppose that your Web Apps is deployed as
Execute as: Me
andWho has access to the app: Anyone
. If your actual setting is different from this, the below script might not be able to be used. Please be careful about this.In the case of your showing
doPost
, no value is returned. And, I’m worried that even whendoPost
is run with the event objecte
, the log might not be shown in the log. But, if your Google Apps Script project has already been linked with the Google Cloud Platform project, the log can be seen. Ref (Author: me)In your Javascript,
{ optionsin }
offetch(url, { optionsin })
is the same with{ optionsin: optionsin }
. In this case, the values inoptionsin
is not used. I guessed that this might be the reason for your current issue ofScript function not found: doGet
.When these points are reflected in your script, how about the following modification?
Modified script:
Google Apps Script side:
Javascript side:
Please set your Web Apps URL to
url
.Testing:
When this Javascript is run, the following value is given as the event object
e
. Also, this value is also returned to Javascript byreturn ContentService.createTextOutput(JSON.stringify(e))
. You can see it in the console.Note:
When you modify the Google Apps Script of Web Apps, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful about this.
You can see the details of this in my report "Redeploying Web Apps without Changing URL of Web Apps for new IDE (Author: me)".
References: