我想知道如何使用 fetch 转换curl命令来执行具有关联输入变量的bash脚本?
以下内容通过控制台完美运行。
curl -s http://localhost:3001/ident.sh | bash /dev/stdin x627306090abab3a6e1400e9345bc60c78a8bef57 2 1019489767657645
但是每当我尝试使用 fetch 调用它时甚至不使用脚本参数:
fetch("http://localhost:3001/ident.sh")
.then((resp) => resp.json())
.then((data) => {
console.log(data)
})
或者
fetch("http://localhost:3001/ident.sh", {
method: 'GET',
headers : {
'Content-Type': 'application/json',
'Accept': 'application/json' }
})
.then((resp) => resp.json()) // Transform the data into json
.then((data) => {
console.log(data)
})
这只会导致:
Uncaught (in promise) SyntaxError: Unexpected token # in JSON at position 0
最佳答案
indent.sh
不返回 JSON,它返回包含 shell 脚本的纯文本。如果您想查看它,请使用 resp.text()
,而不是 resp.json()
。
这不会执行脚本。请参阅Execute a command line binary with Node.js了解如何从 Node.js 执行命令。
关于javascript - 通过URL获取执行本地bash脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50709441/