从 Google Apps 脚本中的 https 调用获取状态文本
Get status text from https call in Google Apps Script
我有一个 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 云函数 (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 将记录 'No email address',而 Google App Script 将记录 204 和 'DOH!'。然而,我得到的是 Google Cloud Function 按预期记录 'No email address',但 Google Apps 脚本记录 204 和空字符串。如果我将 ERR_STATUS_NO_CONTENT 更改为 200,则 Google Apps 脚本日志将按预期包含 200 和 'DOH!'。这是一个错误还是我遗漏了什么?我敢打赌我错过了什么。
查看上一个问题:How to catch UrlFetchApp.fetch exception
You can use the undocumented advanced option "muteHttpExceptions" to disable exceptions when a non-200 status code is returned, and then inspect the status code of the response. More information and an example is available on this issue.
来自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 响应意味着 return 由成功的请求编辑,但并不意味着 return 任何数据。
如果您想 return 成功 HTTP 响应的数据,请考虑使用以下任何状态代码:
我有一个 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 云函数 (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 将记录 'No email address',而 Google App Script 将记录 204 和 'DOH!'。然而,我得到的是 Google Cloud Function 按预期记录 'No email address',但 Google Apps 脚本记录 204 和空字符串。如果我将 ERR_STATUS_NO_CONTENT 更改为 200,则 Google Apps 脚本日志将按预期包含 200 和 'DOH!'。这是一个错误还是我遗漏了什么?我敢打赌我错过了什么。
查看上一个问题:How to catch UrlFetchApp.fetch exception
You can use the undocumented advanced option "muteHttpExceptions" to disable exceptions when a non-200 status code is returned, and then inspect the status code of the response. More information and an example is available on this issue.
来自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 响应意味着 return 由成功的请求编辑,但并不意味着 return 任何数据。
如果您想 return 成功 HTTP 响应的数据,请考虑使用以下任何状态代码: