Ehs*_*n88 1 javascript http-status-codes google-apps-script
这是doPost
Google 应用程序内的一个函数,可返回Hello World消息。
function doPost(e){
return ContentService.createTextOutput('Hello World');
}
Run Code Online (Sandbox Code Playgroud)
现在假设我只想接受发布到此 Google App 端点的有效 JSON,并且我想发送带有错误请求状态的响应。我怎样才能做到这一点。这是伪代码:
function doPost(e){
try{
const data = JSON.parse(e.postData.contents);
return ContentService.createTextOutput('Hello World');
}catch(err){
// Send Bad Request
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,在现阶段,ContentService
无法修改状态码。当我查看Class ContentService的官方文档时,找不到这样的方法。Ref看来这是当前的规范。
因此,在您的情况下,作为当前的解决方法,将值作为 JSON 数据返回怎么样?这样,您就可以使用 JSON 数据的键来检查该值。例如,下面的示例脚本怎么样?
当返回没有错误的正确值时,
return ContentService.createTextOutput(JSON.stringify({value: 'value'}));
Run Code Online (Sandbox Code Playgroud)
当返回有错误的值时,
return ContentService.createTextOutput(JSON.stringify({error: 'Error message'}));
Run Code Online (Sandbox Code Playgroud)
当您需要时.setMimeType(ContentService.MimeType.JSON)
,请添加此内容。
归档时间: |
|
查看次数: |
1602 次 |
最近记录: |