I’m using nodes child-process
with its exec
function like this.
async function run (input: any) {
console.log("======" + input)
const ret = await exec(input, { cwd: 'src/lib/utils' });
const output = {stdout: ret.stdout, stderr: ret.stderr, error: ret.error}
return output
// return ret.stdout, ret.stderr, ret.error
}
Basically it takes a command that looks like this that runs a linux command curl
curl 'https://localhost:8289/v1/request'
--header 'Content-Type: application/json'
-H "Authorization: Bearer $(<token)"
--data '{
"request": "'"$(<data)"'"
}' -k -s
I need to interpolate some variables namely a token
and some data. However since the data is JSON I need to include some additional characters for escaping. This command works fine from the command line however when it’s called using child-process exec
it fails with this error.
{"message":"Syntax error: offset=59, error=invalid character '\n' in string literal"}
If I include the $(<data) directly inline it works fine. Obviously the escape characters are causing the problem here.