Google Apps脚本:使用cURL测试doPost() [英] Google Apps Script: testing doPost() with cURL
问题描述
我创建了一个非常基本的Google Apps脚本。这是我的 doPost()
和 doGet()
:
I have created a very basic Google Apps Script. Here is my doPost()
and doGet()
:
function doPost(e) {
return respond(JSON.stringify({result: "Hello world!"}));
}
function doGet(e) {
return respond(JSON.stringify({result: "Hello world!"}));
}
function respond(response) {
return ContentService
.createTextOutput(response)
.setMimeType(ContentService.MimeType.JSON)
}
我已经用新版本部署了该应用程序。它设置为像我一样运行,但是每个人都可以运行它,包括匿名运行。
I have deployed the app with a new version. It is set to run as me but everyone can run it including anonymous.
我正在通过从命令行运行cURL来测试终结点,并且对于GET来说,它可以按预期工作:
I am testing the endpoint by running cURL from the command line, and for GET it works as expected:
请求-
curl -L https://script.google.com/macros/s/AKfycbxYxgMfMkR6hGI5UO2Gn8tg369oqsy_W41-olb0Do1y8gOjaNvm/exec
响应-
{"result":"Hello world!"}
但对于POST:
请求-
curl -H "Content-Length: 0" -D "p=p" -X POST -L https://script.google.com/macros/s/AKfycbxYxgMfMkR6hGI5UO2Gn8tg369oqsy_W41-olb0Do1y8gOjaNvm/exec
响应-
Sorry, unable to open the file at this time.
Please check the address and try again.
从命令中可以看到,为了达到这一目标,我不得不进行一些操作:虚拟数据,添加内容长度标头,并添加-L标志以使用重定向。
As you can see from the command, I had to manipulate things a bit to get this far: including dummy data, adding a content-length header, and adding the -L flag to consume the redirect.
但是,我被困住了。为什么不能查找文件?为什么会认为有文件?
However, I am stuck. Why can't it "find the file"? Why does it think there is a file?
推荐答案
我怀疑您需要发送数据。
I suspect you'll need to send data.
这有效(我得到了 { result:收到POST请求!}
):
curl -H 'Content-Type: application/json' -d {} -L https://script.google.com/macros/s/AKfycbxYxgMfMkR6hGI5UO2Gn8tg369oqsy_W41-olb0Do1y8gOjaNvm/exec
这样做是这样的:
curl -d "" -L https://script.google.com/macros/s/AKfycbxYxgMfMkR6hGI5UO2Gn8tg369oqsy_W41-olb0Do1y8gOjaNvm/exec
这篇关于Google Apps脚本:使用cURL测试doPost()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!