将变量传递给 child_process.exec 中的 curl 命令失败
Passing variables to curl command in child_process.exec fails
我试图使用 child_process.exec
通过长命令调用 curl,以便将一些数据发送到 API。类似于以下示例:
exec('git log --oneline | wc -l', function(error, stdin, stderr) {
if (stdin > 1) {
exec('curl -H "Content-Type: application/json" -X POST -d \'{"value1": "\'"$arg"\'"}\' https://https://maker.ifttt.com/trigger/{event}/with/key/<my-key>', { "env" : {"arg": stdin } });
}
})
因此,如果 git 存储库在其 git log
输出中包含多行,那么您将对某些 API 执行 POST 请求(这里,一个简单的ifttt.com 中的 webhook),其中您在过程中传递了一些变量(arg
)。
请注意,这是最好的尝试,但总的来说,我为转义单引号和双引号付出了很多努力。在这种特殊情况下,HTTP 请求未正确发送,因为正文包含换行符:
POST / HTTP/1.1
Host: <some-host>
User-Agent: curl/7.50.1
Accept: */*
Content-Type: application/json
Content-Length: 16
{"value1": "2
"}
最后,我不得不使用外部 bash 脚本:
exec('./send_request.sh $arg', { "env": {"arg": stdin } });
但我仍然很好奇如何让它在同一个 js
文件中工作。
如果有帮助,我是 运行 node 6.11.0 和 curl 7.52.1。
尝试:
exec('git log --oneline | wc -l', function(error, stdin, stderr) {
if (stdin > 1) {
exec('curl -H "Content-Type: application/json" -X POST -d \'{"value1": "\'"$arg"\'"}\' https://https://maker.ifttt.com/trigger/{event}/with/key/<my-key>', { "env" : {"arg": stdin.replace(/\n/g, '') } });
}
})
您的变量 'stdin'(您应该将其重命名为 'stdout')的末尾有一个 \n
它。
我试图使用 child_process.exec
通过长命令调用 curl,以便将一些数据发送到 API。类似于以下示例:
exec('git log --oneline | wc -l', function(error, stdin, stderr) {
if (stdin > 1) {
exec('curl -H "Content-Type: application/json" -X POST -d \'{"value1": "\'"$arg"\'"}\' https://https://maker.ifttt.com/trigger/{event}/with/key/<my-key>', { "env" : {"arg": stdin } });
}
})
因此,如果 git 存储库在其 git log
输出中包含多行,那么您将对某些 API 执行 POST 请求(这里,一个简单的ifttt.com 中的 webhook),其中您在过程中传递了一些变量(arg
)。
请注意,这是最好的尝试,但总的来说,我为转义单引号和双引号付出了很多努力。在这种特殊情况下,HTTP 请求未正确发送,因为正文包含换行符:
POST / HTTP/1.1
Host: <some-host>
User-Agent: curl/7.50.1
Accept: */*
Content-Type: application/json
Content-Length: 16
{"value1": "2
"}
最后,我不得不使用外部 bash 脚本:
exec('./send_request.sh $arg', { "env": {"arg": stdin } });
但我仍然很好奇如何让它在同一个 js
文件中工作。
如果有帮助,我是 运行 node 6.11.0 和 curl 7.52.1。
尝试:
exec('git log --oneline | wc -l', function(error, stdin, stderr) {
if (stdin > 1) {
exec('curl -H "Content-Type: application/json" -X POST -d \'{"value1": "\'"$arg"\'"}\' https://https://maker.ifttt.com/trigger/{event}/with/key/<my-key>', { "env" : {"arg": stdin.replace(/\n/g, '') } });
}
})
您的变量 'stdin'(您应该将其重命名为 'stdout')的末尾有一个 \n
它。