0

I would like to know if it is possible to use curl with a variable. This is the code I am using:

const exec = require('child_process').exec;
function execute(command, callback) {
  var a = exec('curl -F data=@/"doc.xml" https://chowlk.linkeddata.es/api', (error, stdout, stderr) => {
    callback(stdout);
  });
};
execute('ping -c 4 0.0.0.0', (a) => {
  download("a.owl", a);
});

The code works, but where it says doc.xml I want to put a variable where I have previously saved the xml. I have tried different ways but I have not succeeded. Is it possible to pass a variable to curl?

H_2525
  • 11
  • 2
  • This is not about passing a variable to curl at all. It is about inserting a string into another string. curl doesn't matter here. Also, the code you have is invalid, you cannot wrap something in double quotes inside a double quoted string. –  May 08 '21 at 08:10
  • `exec("curl -F data=@/\"" + yourvariable + "\" https…")` or ``exec(`curl -F data=@/"${yourvariable}" https…`)`` (make sure you control the source of the variable's value, otherwise: hello command injection) – knittl May 08 '21 at 08:11
  • 4
    Duplicate [How to insert variables in JavaScript strings?](https://stackoverflow.com/questions/19105009/how-to-insert-variables-in-javascript-strings) –  May 08 '21 at 08:12
  • Also note that your code is completely broken. There's no download function to be seen, and the ping string is just discarded, since the execute function isn't using the `command` parameter anywhere. –  May 08 '21 at 08:14
  • The code as currently presented has a syntax error. `exec("curl data=@/"doc.xml" https…")` is invalid syntax – knittl May 08 '21 at 11:06
  • 1
    Just curious. Using HTTPS. Why using curl and not using ajax? – armagedescu May 08 '21 at 13:20

0 Answers0