3

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.net/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 (command /*, options, callback */) {
  var file, args, options, callback;
6
  • What version of Node are you running? I ran this exact script, with the just the URL and got a response 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 Commented Oct 24, 2016 at 6:40
  • I'm using the version v0.12.10 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).. Commented Oct 24, 2016 at 6:45
  • @NextLocal please show me how to use node-curl Commented Oct 24, 2016 at 6:45

2 Answers 2

3

Your server is not supported CURL. It will better if you use a request library https://github.com/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.net/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);
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

I use request. It's great
The OP is using CURL for a POST, with JSON data with a username and password. Your answer does not show how to do all that with the request() library which is the operative part of the question.
3

Simple example with request

var request = require('request');
request('http://www.httpbin.org', 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.org');
curl.setOpt( Curl.option.FOLLOWLOCATION, true );
curl.setOpt( Curl.option.HTTPPOST, [
    { name: 'login', contents: 'username' }
]);
curl.perform();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.