问题描述
这个问题是关于从某个地方接收POST请求的.我正在寻找一个Google工作表脚本函数,该函数可以从JSON格式的POST请求中获取和处理数据.你能举个例子吗?
This question is about receiving POST request from somewhere. I'm looking for a google sheet script function that can take and handle data from the POST request in JSON format. Could you suggest any example?
POST请求在这里:
The POST request is here:
https://script.google.com/macros/s/BOdirjv45Dn6FHrx_4GUguuS6NJxnSEeviMHm3HerJl4UsDBnDgfFPO/
{
"p1": "writeTitle",
"p2": [[URL]],
"p3": [[PIC_A]],
"p4": [[PIC_B]],
"p5": [[TITLE]]
}
application/json
doPost()
不起作用:
doPost(e) {
var json = JSON.parse(e.postData.contents);
Logger.log(json);
}
推荐答案
- 您要从请求正文中检索作为对象的值.
- 您已经部署了Web Apps.
如果我对您的情况的理解是正确的,那么如何进行修改?
If my understanding of your situation is correct, how about this modification?
作为示例,我使用以下curl命令将POST张贴到Web Apps.
As a sample, I used the following curl command to POST to Web Apps.
curl -L \
-H 'Content-Type:application/json' \
-d '{"p1": "writeTitle","p2": "[[URL]]","p3": "[[PIC_A]]","p4": "[[PIC_B]]","p5": "[[TITLE]]"}' \
"https://script.google.com/macros/s/#####/exec"
运行上述命令时,doPost(e)
中的e
如下.
When above command is run, e
of doPost(e)
is as follows.
{
"parameter": {},
"contextPath": "",
"contentLength": 90,
"queryString": "",
"parameters": {},
"postData": {
"type": "application/json",
"length": 90,
"contents": "{\"p1\": \"writeTitle\",\"p2\": \"[[URL]]\",\"p3\": \"[[PIC_A]]\",\"p4\": \"[[PIC_B]]\",\"p5\": \"[[TITLE]]\"}",
"name": "postData"
}
}
已发布的有效负载可以由e.postData
检索.从上面的响应中发现,您可以通过e.postData.contents
检索所需的值.顺便说一句,当查询参数和有效载荷如下给出时,
The posted payload can be retrieved by e.postData
. From above response, it is found that the value you want can be retrieved by e.postData.contents
. By the way, when the query parameter and the payload are given like as follows,
curl -L \
-H 'Content-Type:application/json' \
-d '{"p1": "writeTitle","p2": "[[URL]]","p3": "[[PIC_A]]","p4": "[[PIC_B]]","p5": "[[TITLE]]"}' \
"https://script.google.com/macros/s/#####/exec?key=value"
value
可以通过e.parameter
或e.parameters
检索.有效载荷可以通过e.postData.contents
检索.
value
can be retrieved by e.parameter
or e.parameters
. And the payload can be retrieved by e.postData.contents
.
在此修改后的脚本中,可以在Stackdriver上看到结果,并返回结果.
In this modified script, the result can be seen at the Stackdriver, and also the result is returned.
function doPost(e) {
var json = JSON.parse(e.postData.contents);
console.log(json);
return ContentService.createTextOutput(JSON.stringify(json));
}