javascript 如何在 exec nodejs 中使用 curl

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27189849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:05:49  来源:igfitidea点击:

How to use curl with exec nodejs

javascriptnode.jscurl

提问by anders

I try to do the following in node js

我尝试在节点 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

当我在命令行中执行以下操作时,它会起作用。:
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

但是当我在 nodejs 中这样做时,它告诉我

curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information
child process exited with code 2

回答by Baart

The optionsparameter of the exec command is not there to contains your argv.

optionsexec 命令的参数不包含您的 argv。

You could put your parameters directly with the child_process.execfunction :

您可以将参数直接与child_process.exec函数一起放置:

    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,

如果要使用 argv 参数,

you can use child_process.execFilefunction:

您可以使用child_process.execFile功能:

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);
    }
});

回答by Brad Parks

You can do it like so... You can easily swap out execSyncwith execas in your above example.

你可以像这样...您可以轻松更换execSyncexec在你上面的例子。

#!/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);

回答by mscdex

FWIW you can do the same thing natively in node with:

FWIW 你可以在 node 中本地做同样的事情:

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));