admin管理员组文章数量:1187042
I try to do the following in node js
var command = " -d '{'title': 'Test' }' -H 'Content-Type: application/json' http://125.196.19.210:3030/widgets/test";
exec(['curl', command], function(err, out, code) {
if (err instanceof Error)
throw err;
process.stderr.write(err);
process.stdout.write(out);
process.exit(code);
});
It works when I do the below in command line.:
curl -d '{ "title": "Test" }' -H "Content-Type: application/json" http://125.196.19.210:3030/widgets/test
But when I do it in nodejs it tells me that
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information
child process exited with code 2
I try to do the following in node js
var command = " -d '{'title': 'Test' }' -H 'Content-Type: application/json' http://125.196.19.210:3030/widgets/test";
exec(['curl', command], function(err, out, code) {
if (err instanceof Error)
throw err;
process.stderr.write(err);
process.stdout.write(out);
process.exit(code);
});
It works when I do the below in command line.:
curl -d '{ "title": "Test" }' -H "Content-Type: application/json" http://125.196.19.210:3030/widgets/test
But when I do it in nodejs it tells me that
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information
child process exited with code 2
Share
Improve this question
asked Nov 28, 2014 at 13:34
andersanders
1,5455 gold badges20 silver badges44 bronze badges
1
- Is this issue resolved ? – Baart Commented Apr 15, 2015 at 15:54
3 Answers
Reset to default 11You can do it like so... You can easily swap out execSync
with exec
as in your above example.
#!/usr/bin/env node
var child_process = require('child_process');
function runCmd(cmd)
{
var resp = child_process.execSync(cmd);
var result = resp.toString('UTF8');
return result;
}
var cmd = "curl -s -d '{'title': 'Test' }' -H 'Content-Type: application/json' http://125.196.19.210:3030/widgets/test";
var result = runCmd(cmd);
console.log(result);
The options
parameter of the exec command is not there to contains your argv.
You could put your parameters directly with the child_process.exec
function :
var exec = require('child_process').exec;
var args = " -d '{'title': 'Test' }' -H 'Content-Type: application/json' http://125.196.19.210:3030/widgets/test";
exec('curl ' + args, function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
If you want to use the argv arguments,
you can use child_process.execFile
function:
var execFile = require('child_process').execFile;
var args = ["-d '{'title': 'Test' }'", "-H 'Content-Type: application/json'", "http://125.196.19.210:3030/widgets/test"];
execFile('curl.exe', args, {},
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
FWIW you can do the same thing natively in node with:
var http = require('http'),
url = require('url');
var opts = url.parse('http://125.196.19.210:3030/widgets/test'),
data = { title: 'Test' };
opts.headers = {};
opts.headers['Content-Type'] = 'application/json';
http.request(opts, function(res) {
// do whatever you want with the response
res.pipe(process.stdout);
}).end(JSON.stringify(data));
本文标签: javascriptHow to use curl with exec nodejsStack Overflow
版权声明:本文标题:javascript - How to use curl with exec nodejs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738358569a2080299.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论