-2

I need to create a function in javascript where I can execute curl command:

curl -v -X POST -k -u testUser -d login=testUser2 -d password=123456

when i run above command in terminal it will prompt me the password, where I have the to enter the password and then I can login into the database.

I am new to Javascript so I don't know how to do that.

I read some article related to exec and child process command in Javascript but I didn't find any example that will show how I can implement said behavior. I am adding below the sample that I found on internet:

const { exec } = require('child_process');

export const executeCommand = (cmd, successCallback, errorCallback) => {
  exec(cmd, (error, stdout, stderr) => {
    if (error) {
     // console.log(`error: ${error.message}`);
      if (errorCallback) {
        errorCallback(error.message);
      }
      return;
    }
    if (stderr) {
      //console.log(`stderr: ${stderr}`);
      if (errorCallback) {
        errorCallback(stderr);
      }
      return;
    }
    //console.log(`stdout: ${stdout}`);
    if (successCallback) {
      successCallback(stdout);
    }
  });
};

1 Answers1

0

If you really want to do it this way write instead of cmd your command.

const { exec } = require("child_process");

exec("curl -v -X POST -k -u testUser:123456 -d login=testUser2 
", (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
});
Aalexander
  • 4,987
  • 3
  • 11
  • 34
  • but how I will pass the inline password when i run the code, because when I run the curl command in terminal it ask me for password – Pranjal Prakash Srivastava Dec 22 '20 at 21:47
  • you can try this You can also include a password in the command, but your password will appear in the bash history: curl -u username:password http://example.co – Aalexander Dec 22 '20 at 21:55
  • curl -u username:password, then it will not prompt for the password. https://programming.vip/docs/use-a-curl-with-a-username-and-password.html – Aalexander Dec 22 '20 at 21:55