javascript - 通过URL获取执行本地bash脚本

标签 javascript bash fetch-api

我想知道如何使用 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/

相关文章:

javascript - 为什么bind会修复“"failed to execute ' fetch' on 'Window'非法调用”错误?

javascript - Jquery - 嵌套列表的自动递增 ID

javascript - JS : How can I add items to an new Objects array, 从构造函数实例化?

javascript - 将 HTML Canvas 中的像素颜色处理为另一种颜色但具有相同的阴影

linux - 我想通过 shell 删除所有文件,除了少数 as 但导致语法错误 : "(" unexpected

perl - 如何从列表中删除可以在列表中其他较长行中找到的行?

bash - 将字符串参数从 bash 脚本传递到 matlab 文件的最接近方法是什么?

c# - 关于编写更具可读性的代码的建议?

javascript - 如何使用 "Fetch API"在javascript和c#之间传递数据?

javascript - 从 reddit API 中提取高分辨率图像