I
I
Ingvar Von Bjork2021-03-09 15:28:41
Node.js
Ingvar Von Bjork, 2021-03-09 15:28:41

How to wait for curl to complete?

There is an example code like this:

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

var cmd = `curl -X POST https://secure.payu.com/api/v2_1/orders -H "Content-Type: application/json" -H "Authorization: Bearer 3e5cac39-7e38-4139-8fd6-30adc06a61bd" -d '{ "notifyUrl": "https://your.eshop.com/notify", "customerIp": "127.0.0.1", "merchantPosId": "145227", "description": "RTV market", "currencyCode": "PLN", "totalAmount": "21000", "buyer": {     "email": "[email protected]",     "phone": "654111654",     "firstName": "John",     "lastName": "Doe",     "language": "pl" }, "products": [     {         "name": "Wireless Mouse for Laptop",         "unitPrice": "15000",         "quantity": "1"     },     {         "name": "HDMI cable",         "unitPrice": "6000",         "quantity": "1"     } ]}'`;

function execCmd() {
  this.execCommand = function (cmd, callback) {
    exec(cmd, (error, stdout, stderr) => {
      if (error) {
        console.error(`error: ${error}`);
        return;
      }

      if (stderr) {
        console.error(`stderr: ${stderr}`);
        return;
      }

      callback(stdout);
    });
  };
}

var os = new execCmd();

os.execCommand(cmd, function (result) {
  console.log(result);
});

Executing the code only outputs to the console
stderr:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1098  100   551  100   547   1088   1081 --:--:-- --:--:-- --:--:--  2169

This is probably due to the fact that js completes work even before curl has time to fully go. Is it possible to wait until curl completes?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly, 2021-03-09
@vshvydky

it’s better not to use curl, there is fetch, got, axios , request

S
Saboteur, 2021-03-09
@saboteur_kiev

The console displays the progress that curl writes to stderr
to stdout, in theory, it does not write anything, you have a POST

X
XenonDev, 2021-03-26
@XenonDev

try converting to promises

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

const cmd = `curl -X POST https://secure.payu.com/api/v2_1/orders -H "Content-Type: application/json" -H "Authorization: Bearer 3e5cac39-7e38-4139-8fd6-30adc06a61bd" -d '{ "notifyUrl": "https://your.eshop.com/notify", "customerIp": "127.0.0.1", "merchantPosId": "145227", "description": "RTV market", "currencyCode": "PLN", "totalAmount": "21000", "buyer": {     "email": "[email protected]",     "phone": "654111654",     "firstName": "John",     "lastName": "Doe",     "language": "pl" }, "products": [     {         "name": "Wireless Mouse for Laptop",         "unitPrice": "15000",         "quantity": "1"     },     {         "name": "HDMI cable",         "unitPrice": "6000",         "quantity": "1"     } ]}'`;

execCommand(cmd)
  .then(({ stdout, stderr }) => console.log(`stdout: ${stdout}\nstderr: ${stderr}`))
  .catch(err => console.error(err)
  .finally(() => console.log('App has been finished'))

function execCommand(cmd) {
    return new Promise(resolve, reject) {
      exec(cmd, (error, stdout, stderr) => error? reject(error) : resolve({ stdout, stderr }));
  };
}

And to be honest, I don’t know how to find out whether curl was successfully completed or not, since stderr will always come. Perhaps you can look at the return code of the process ...
In general, they wrote you correctly, you need to use something from the libs (requset, axios, fetch ...) to send requests. If you don't want an external lib, you can use the native http library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question