Answer the question
In order to leave comments, you need to log in
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);
});
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
Answer the question
In order to leave comments, you need to log in
The console displays the progress that curl writes to stderr
to stdout, in theory, it does not write anything, you have a POST
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 }));
};
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question