node.js - 从 Google Apps 脚本中的 https 调用获取状态文本

标签 node.js google-apps-script httprequest

我有一个 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/

相关文章:

java - Spring MVC 展示中如何使用 JSON 处理 HTTP 请求?一些需要理解的问题

node.js - 如何连接solr和nodejs?

mysql - 如何使用谷歌表格数据自动更新 SQl 数据库

java - 使用 Java 发送 HTTP Post Payload

javascript - 从单个 HTTP 请求生成多个 HTTP 请求 Angular 2

javascript - 使用谷歌应用程序脚本获取多选选项值

javascript - Chrome 扩展程序 : sync localstorage with server api

node.js - Azure Function Node.js 无法为运行时启动新的语言工作线程 : node

node.js - 快速重定向不会改变 req.url

google-apps-script - 如何使用 Google 电子邮件设置 API 和 OAuth2 for Apps 脚本库为 Google Apps 域中的用户设置电子邮件签名