javascript - 如何让 Google Apps 脚本 doPost() 读取我的 POST 请求?
问题描述
我正在使用 node.js 和 Puppeteer 从无头浏览器运行一个抓取项目。我想将数据发布到 Google Apps 脚本以进行进一步处理。我希望在我的 GAS 项目中看到填充参数的数据。但相反,我得到以下结果,只有空参数。
https://script.googleusercontent.com/macros/echo?user_content_key=[key]{"parameter":{},"contextPath":"","contentLength":-1,"queryString":"","parameters":{}}
这是生成该响应的 GAS 代码。
代码.gsfunction doGet(e){
return handleResponse(e);
}
function doPost(e){
return handleResponse(e);
}
function handleResponse(e) {
var json = JSON.stringify(e)
var textOutput = ContentService.createTextOutput(json);
return textOutput
}
这是我用来发送请求的代码。
刮刀.jsconst request = require('request');
request({
method: POST,
preambleCRLF: true,
postambleCRLF: true,
uri: postUrl,
multipart: {
chunked: false,
data,
},
},
我已使用 [RequestBin][1] 验证我正在发送有效的 POST 请求。
我究竟做错了什么?
解决方案
请查看您发布网络应用程序的方式。
- 执行应用程序为:我
- 谁有权访问该应用程序:任何人,甚至是匿名的
您的 POST 和 GET 的 URL 应该类似于https://script.google.com/macros/s/IDENTIFIER/exec
,而不是https://script.googleusercontent.com/macros/echo?user_content_key=[key]
。后一个 URL 看起来像是来自发布“任何人,甚至匿名”都无法访问的 Web 应用程序的重定向。
如果设置正确,此请求
curl --request POST \
--url 'https://script.google.com/macros/s/IDENTIFIER/exec?param1=1¶m2=2' \
--header 'content-type: application/json' \
--data '{"json": true}'
返回
{
"parameter": {
"param1": "1",
"param2": "2"
},
"contextPath": "",
"contentLength": 14,
"queryString": "param1=1¶m2=2",
"parameters": {
"param1": [
"1"
],
"param2": [
"2"
]
},
"postData": {
"type": "application/json",
"length": 14,
"contents": "{\"json\": true}",
"name": "postData"
}
}
推荐阅读
- azure - 从 Android KitKat 调用 Azure IoT 文件上传返回 BAD_FORMAT
- javascript - Javascript 停止加载脚本,如果用户同意,可能会再次加载脚本
- javascript - 如何在 ES6 中的两个扩展运算符之间设置逻辑运算符
- html - 具有默认宽度的灵活 textarea 组件
- bash - 用户有没有办法指定我的脚本将搜索多少个文件夹级别?
- amazon-web-services - 批量替换 Terraform 中的文件内容
- c# - 有没有一种简单的方法来表示子组件中发生的数据更改?
- python - 如何在不使用和拆分测试集的情况下将我的数据集拆分为训练和验证?
- python - 在 Dataframes (Python) 中使用“For”和“If”的问题
- python - Python 3.6.1 找不到包和模块 [import]