0

I'm wondering how do i convert a curl command to execute a bash script with associating input vars by utilising fetch?

The following works perfectly through the console.

  curl -s http://localhost:3001/ident.sh | bash /dev/stdin x627306090abab3a6e1400e9345bc60c78a8bef57 2 1019489767657645

But then whenever I try to call it using fetch and even without the script arguments:

  fetch("http://localhost:3001/ident.sh")
  .then((resp) => resp.json())
  .then((data) => {

      console.log(data)

  })

OR

  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)

  })

Which just results in:

Uncaught (in promise) SyntaxError: Unexpected token # in JSON at position 0
Vasan
  • 4,810
  • 4
  • 20
  • 39
Samuel Gosling
  • 367
  • 1
  • 2
  • 7
  • Why are you calling `resp.json()`? It's not JSON, it's just plain text that contains a shell script. – Barmar Jun 05 '18 at 21:54

1 Answers1

2

indent.sh is not returning JSON, it's returning plain text that contains a shell script. If you want to see it, use resp.text(), not resp.json().

This won't execute the script. See Execute a command line binary with Node.js for how to execute a command from Node.js.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks, facing some lovely problems on that end is the only problemo. Cheers for pointing me in the right direction regardless. – Samuel Gosling Jun 06 '18 at 12:04