GAS 错误:“未找到脚本函数:doPost”

问题描述 投票:0回答:1

我有两个 GAS 脚本,

send.gs
respond.gs

send.gs
:只需发送POST请求到respond.gs

respond.gs
:部署并发布为 WebApp,并且仅响应
hello,world

send.gs
使用
respond.gs
UrlFetchApp.fetch
发送POST请求时,
GAS Error :"Script function not found: doPost"
出现错误:

[

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);
}


很多人注意到GAS向WebApp发送HTTP请求的方式与普通的有点不同。 就像,当使用curl发送POST请求时,使用

-d " "
而不是
-X POST

但是,我找不到使用

UrlFetchApp.fetch
向GAS的WebApp发送HTTP请求的方法。

如有任何信息,我们将不胜感激。

javascript http google-apps-script post urlfetch
1个回答
0
投票

我通过再次部署并指定

New Version
来实现它。

向 Web 应用程序发出 GET 和 POST 请求时出现“找不到函数”错误

© www.soinside.com 2019 - 2024. All rights reserved.