我有两个 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请求的方法。
如有任何信息,我们将不胜感激。