admin管理员组文章数量:1295948
I'm using the following code to execute a rest API using Curl with Node.JS. It is working on my local machine.
...
...
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 -u userx:passx -X POST --data @test.json -H 'Content-Type: application/json' /";
var result = runCmd(cmd);
...
...
But after uploading to server, I'm getting the following error
/home/ubuntu/PMPBuild/jiraIssue.js:76
var resp = child_process.execSync(cmd);
^
TypeError: Object function (mand /*, options, callback */) {
var file, args, options, callback;
I'm using the following code to execute a rest API using Curl with Node.JS. It is working on my local machine.
...
...
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 -u userx:passx -X POST --data @test.json -H 'Content-Type: application/json' https://mysite.atlassian/rest/api/2/issue/";
var result = runCmd(cmd);
...
...
But after uploading to server, I'm getting the following error
/home/ubuntu/PMPBuild/jiraIssue.js:76
var resp = child_process.execSync(cmd);
^
TypeError: Object function (mand /*, options, callback */) {
var file, args, options, callback;
Share
edited Oct 24, 2016 at 6:35
Jobi
asked Oct 24, 2016 at 6:31
JobiJobi
1,1225 gold badges26 silver badges38 bronze badges
6
- What version of Node are you running? I ran this exact script, with the just the URL and got a response – nikjohn Commented Oct 24, 2016 at 6:38
-
I can show you how to use
node-curl
library if you can't get this working, just let me know – jaggedsoft Commented Oct 24, 2016 at 6:40 - I'm using the version v0.12.10 – Jobi Commented Oct 24, 2016 at 6:42
- 1 node.js v0.12.10 is way, way out of date. You should be running either 4.x or 6.x (preferably 6.x).. – jfriend00 Commented Oct 24, 2016 at 6:45
- @NextLocal please show me how to use node-curl – Jobi Commented Oct 24, 2016 at 6:45
2 Answers
Reset to default 3Your server is not supported CURL. It will better if you use a request library https://github./request/request
var request = require('request');
var jsonData;
fs = require('fs')
fs.readFile('test.json', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
jsonData = JSON.parse(data);
});
request({
url: 'https://mysite.atlassian/rest/api/2/issue/',
'auth': {
'user': 'userx',
'pass': 'passx',
'sendImmediately': false
},
qs: jsonData,
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}, function(error, response, body){
if(error) {
console.log(error);
} else {
console.log(response.statusCode, body);
}
});
Simple example with request
var request = require('request');
request('http://www.httpbin', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
})
Example on how to use node-libcurl
var Curl = require('node-libcurl').Curl;
var curl = new Curl();
curl.setOpt( Curl.option.URL, 'http://www.httpbin');
curl.setOpt( Curl.option.FOLLOWLOCATION, true );
curl.setOpt( Curl.option.HTTPPOST, [
{ name: 'login', contents: 'username' }
]);
curl.perform();
本文标签: javascriptCurl with NodeJSStack Overflow
版权声明:本文标题:javascript - Curl with NodeJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741626398a2389108.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论