GAS Errror :"Script function not found: doPost"

25 Views Asked by At

I have two GAS scripts, send.gs and respond.gs.

send.gs: Just sends POST request to respond.gs

respond.gs: Deployed and published as WebApp and just responds hello,world

When send.gs sends POST request to respond.gs using UrlFetchApp.fetch, the error GAS Error :"Script function not found: doPost" comes:

[send.gs]

function sendPostRequest() {
  var data = {
    'name': 'Bob Smith',
    'age': 35,
    'pets': ['fido', 'fluffy']
  };
  var headers = {
    "key":"hehehe"
  };
  var options = {
    "headers": headers,
    "Content-Type": "application/json",
    "method": "post",
    "payload": JSON.stringify(data)
  };

  const response = UrlFetchApp.fetch("https://script.google.com/macros/s/<foo>/exec", options);
  console.log(response.getContentText())
}

/* log


<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="//ssl.gstatic.com/docs/script/images/favicon.ico">
<title>Error</title>
<style type="text/css" nonce="5e3t2FU97EMzHFJstP-Jug">body {background-color: #fff; margin: 0; padding: 0;}.errorMessage {font-family: Arial,sans-serif; font-size: 12pt; font-weight: bold; line-height: 150%; padding-top: 25px;}</style>
</head>
<body style="margin:20px">
<div><img alt="Google Apps Script" src="//ssl.gstatic.com/docs/script/images/logo.png"></div>
<div style="text-align:center;font-family:monospace;margin:50px auto 0;max-width:600px">Script function not found: doPost</div>
</body>
</html>

[respond.gs]

function doPost(e){
  const payload = JSON.stringify({
    method: "POST",
    message: "hello,world",
  });
  return ContentService.createTextOutput(payload).setMimeType(ContentService.MimeType.JSON);
}


Many people note that the way of sending HTTP request to WebApp of GAS is a bit different from ordinary one. Like, when sending POST request using curl, use -d " " instead of -X POST.

However, I could not find way of using UrlFetchApp.fetch for sending HTTP request to WebApp of GAS.

Any information would be appreciated.

1

There are 1 best solutions below

0
ten On

I made it by deploying again with designating New Version.

"function not found" error when making both GET and POST requests to a Web App