我有一个 Google Apps 脚本:
function MakeHTTPCall() {
let resp = UrlFetchApp.fetch('https://something.cloudfunctions.net/returnStatusText');
console.log('Response code: ' + JSON.stringify(resp.getResponseCode()));
console.log('Returned text: ' + JSON.stringify(resp.getContentText()));
return;
}
以及 Google Cloud Function (node.js 8)
exports.returnStatusText = async(req, res) => {
// Return codes
const ERR_OK = 200;
const ERR_STATUS_NO_CONTENT = 204;
// Get the email address sent
if ('undefined' === typeof req.body.emailAddress) {
// email address is undefined
console.log('No email address');
return res.status(ERR_STATUS_NO_CONTENT).send('DOH!');
}
return res.status(ERR_OK).send('OK');
}
我的主要引用是https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
我期望 Google Cloud Function 将记录“无电子邮件地址”,而 Google App 脚本将记录 204 和“DOH!”。然而,我得到的是 Google Cloud Function 按预期记录“无电子邮件地址”,但 Google Apps 脚本记录 204 和空字符串。如果我将 ERR_STATUS_NO_CONTENT 更改为 200,则 Google Apps 脚本日志将显示 200 和“DOH!”正如预期的那样。这是一个错误还是我错过了什么?我敢打赌我错过了一些东西。
最佳答案
来自MDN web docs :
204 No Content
The HTTP 204 No Content success status response code indicates that the request has succeeded, but that the client doesn't need to go away from its current page. A 204 response is cacheable by default. An ETag header is included in such a response.
本质上 - 204 响应意味着已成功的请求返回,但并不意味着返回任何数据。
如果您希望通过成功的 HTTP 响应返回数据,请考虑使用以下任一状态代码:
关于node.js - 从 Google Apps 脚本中的 https 调用获取状态文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59178953/