这是一个doPost
Google App 内的函数返回一个你好世界信息。
function doPost(e){
return ContentService.createTextOutput('Hello World');
}
现在假设我只想接受发布到此 Google App 端点的有效 JSON,并且我想发送带有错误请求状态的响应。我怎样才能做到这一点。这是伪代码:
function doPost(e){
try{
const data = JSON.parse(e.postData.contents);
return ContentService.createTextOutput('Hello World');
}catch(err){
// Send Bad Request
}
}
问题和解决方法:
不幸的是,在现阶段,ContentService
无法修改状态码。当我查看Class ContentService的官方文档时,找不到这样的方法。Ref https://developers.google.com/apps-script/reference/content/content-service看来这是当前的规范。
因此,在您的情况下,作为当前的解决方法,将值作为 JSON 数据返回怎么样?这样,您就可以使用 JSON 数据的键来检查该值。例如,下面的示例脚本怎么样?
-
当返回没有错误的正确值时,
return ContentService.createTextOutput(JSON.stringify({value: 'value'}));
-
当返回有错误的值时,
return ContentService.createTextOutput(JSON.stringify({error: 'Error message'}));
-
当你需要的时候.setMimeType(ContentService.MimeType.JSON)
,请添加此内容。
Note:
- 当我在 Google 问题跟踪器中搜索此问题时,我找不到它。那么,将其报告为未来的请求怎么样?Ref https://developers.google.com/issue-tracker
参考:
- 类内容服务 https://developers.google.com/apps-script/reference/content/content-service
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)