是否可以强制 google 脚本返回 404 错误?

is it possible to force google Script returning 404 error?

将 Google 脚本发布为 webApp 时,允许每个脚本有一个 GET 和一个 POST HTTP 方法。

显然 GET 是获取一些数据。但是,当您尝试获取特定值(例如,使用过滤器参数)但未找到任何值时,如何强制脚本 return 出现 404 错误 - 未找到?

我认为有两种可能:

我认为您目前无法在 GAS 中强制设置响应代码,但您的第二个选项听起来可行。像这样的东西可能对你有用:

function doGet(e){

  var json = handleResponse(e);


  return ContentService.createTextOutput(JSON.stringify(json))
                       .setMimeType(ContentService.MimeType.JSON);
}

处理函数示例:

function handleResponse(e) {

  var arr = [1, 2, 3, 4, 5, 6, 7, 8];
  var param = e.parameter.yourParameter;

  if (param) {

     arr = arr.filter(function(element){

        return element > parseInt(param);

});

   if (arr.length > 0) {

          return {"result":arr};

       }  else {

           return {"error":"no value was found"};

           }

} else {

   return {"error":"query parameter not specified"};

}

}

在接收端,您可以根据其中包含的 属性 处理响应:

if ("result" in response) {
   //do something
}

if ("error" in response) {
   //do something
}