2

How can we connect to Dash DB using HTTP/Request module of node.js and execute R-script code. I am able to do it using Node-RED but like to do it programmatically. Right now I am getting this error:

0530 [App/0] ERR Potentially unhandled rejection [2] Error: EACCES, mkdir '/home/nol' 
0530 [App/0] OUT STATUS: 500 
0530 [App/0] OUT HEADERS: {""content-type":"text/html; charset=UTF-8","set-cookie":Path=/; Secure; HttpOnly"],"connection":"Close", ""} 
0530 [App/0] OUT BODY: 
0530 [App/0] OUT An internal error has occurred. The application may still be initializing or the URL used is invalid. Check the URL and try again. For more information, view the server log files.

Below is my code :

  var options = {
  hostname: 'bluemix05.bluforcloud.com',
  host:'50.97.93.115',
  port: 8443,
  path: ':/console/blushiftservices/BluShiftHttp.do',
  method: 'POST',
  username: 'xxxxxxxxx',
  password: 'xxxxxxxxx',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Authorization' : 'Basic ' + new Buffer(username + ':' + password).toString('base64')
  }
};
var postData = querystring.stringify({
  'msg' : 'cmd=RScriptRunScript&command=library\(ibmdbR\)\ncon <- idaConnect(\"BLUDB\",\"\",\"\")\nidaInit(con)\nSUB_NUM <- c\(0877777777\)\nPAST_YR_AVG <- c\(300\)\nTestUsageTable1<- data.frame \(SUB_NUM,PAST_YR_AVG,stringsAsFactors=FALSE\)\nsqlSave\(con, TestUsageTable1, rownames=FALSE,safer=FALSE,append=TRUE\)&profileName=BLUDB'
});
var req = http.request(options, function (res) {
                             console.log('STATUS: ' + res.statusCode);
                             console.log('HEADERS: ' + JSON.stringify(res.headers));
                             res.setEncoding('utf8');
                             res.on('data', function (chunk) {
                               console.log('BODY: ' + chunk);
                             });
                               loggererror.info('STATUS: ' + res.statusCode);
                           });

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
  loggererror.info('Status: ' + e.message);
});

req.write(postData);
req.end();
Alex da Silva
  • 4,552
  • 2
  • 17
  • 25
  • Hi, could you provide more details about the error you are getting? Could you paste the output of "cf logs [your app name] --recent" command? – v.bontempi Oct 13 '15 at 17:12
  • Hi, Below are the details from cf log : 0530 [App/0] ERR Potentially unhandled rejection [2] Error: EACCES, mkdir '/home/nol' 0530 [App/0] OUT STATUS: 500 0530 [App/0] OUT HEADERS: {""content-type":"text/html; charset=UTF-8","set-cookie":Path=/; Secure; HttpOnly"],"connection":"Close", ""} 0530 [App/0] OUT BODY: 0530 [App/0] OUT An internal error has occurred. The application may still be initializing or the URL used is invalid. Check the URL and try again. For more information, view the server log files. – user5440060 Oct 14 '15 at 05:11
  • To be clear, is this a standalone node.js application or does it involve node-red in anyway? Does anything in your code wrapping this fragment reference `/home/nol` (the path mentioned in the error message)? When do you get that error - is it when the above code gets invoked? – knolleary Oct 14 '15 at 08:45
  • Hi, This is a standalone application and does not involve node-red. However, i was separately able to deploy and execute this r-script code through a node-red flow. My code nowhere references /home/nol and the error changes as and when i change the options properties. Yes error is for above code, if i remove it, the error is gone. I tried to go through the HTTP Request module code of nore-red to see how it works but could not find any major differences for these set of arguments. Iam sure iam missing something but not able to figure it out.Thanks! – user5440060 Oct 14 '15 at 09:03

1 Answers1

1

I modified the code and made it more similar to the node-red http code (make sure you update your username, password and url to match your dashDB instance before testing the code):

var https = require("follow-redirects").https;
var urllib = require("url");

var options = urllib.parse('https://awh-yp-small03.services.dal.bluemix.net:8443/console/blushiftservices/BluShiftHttp.do');
options.method = 'POST';
options.headers = {"content-type": "application/x-www-form-urlencoded"};
options.auth = '<username>:<password>';

var postData = 'cmd=RScriptRunScript&command=library\(ibmdbR\)\ncon <- idaConnect(\"BLUDB\",\"\",\"\")\nidaInit(con)\nSUB_NUM <- c\(0877777777\)\nPAST_YR_AVG <- c\(300\)\nTestUsageTable1<- data.frame \(SUB_NUM,PAST_YR_AVG,stringsAsFactors=FALSE\)\nsqlSave\(con, TestUsageTable1, rownames=FALSE,safer=FALSE,append=TRUE\)&profileName=BLUDB';

var req = https.request(options, function (res) {
                             console.log('STATUS: ' + res.statusCode);
                             console.log('HEADERS: ' + JSON.stringify(res.headers));
                             res.setEncoding('utf8');
                             res.on('data', function (chunk) {
                               console.log('BODY: ' + chunk);
                             });
                               //loggererror.info('STATUS: ' + res.statusCode);
                           });

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
  //loggererror.info('Status: ' + e.message);
});

req.write(postData);
req.end();

My result is this:

STATUS: 200
HEADERS: {"x-powered-by":"Servlet/3.0","content-type":"text/json; charset=UTF-8","content-language":"en-US","content-length":"857","set-cookie":["dsweb11082=0000Pv6OYa62mRxurvYa6c2_5of:31d3d6bb-82d3-44ca-8db8-ac929c09de05; Path=/; Secure; HttpOnly"],"connection":"Close","date":"Wed, 14 Oct 2015 22:11:27 GMT","server":"WebSphere Application Server","expires":"Thu, 01 Dec 1994 16:00:00 GMT","cache-control":"no-cache=\"set-cookie, set-cookie2\""}
BODY: 
{"message":"","cmd":"RScriptRunScript","errorMessageCode":"","items":"{\"RModelOutput\":\"\",\"filename\":[],\"workingDirectory\":\"\\\/opt\\\/ibm\\\/dsserver\\\/Config\\\/RModels\\\/Pv6OYa62mRxurvYa6c2_5of\\\/1444860685812\",\"RModelError\":\"Loading required package: RODBC\\nLoading required package: ibmdbR\\nLoading required package: methods\\nLoading required package: MASS\\nLoading required package: grDevices\\nLoading required package: graphics\\nLoading required package: stats\\nLoading required package: utils\\nLoading required package: Matrix\\nLoading required package:
BODY:  arules\\n\\nAttaching package: \\u2018arules\\u2019\\n\\nThe following objects are masked from \\u2018package:base\\u2019:\\n\\n    %in%, write\\n\\nLoading required package: rpart\\nWarning message:\\nclosing unused RODBC handle 1 \\n\"}","resultcode":"success"}
Alex da Silva
  • 4,552
  • 2
  • 17
  • 25
  • Hi Alex, Thanks a lot!! :)..That worked just fine. The "follow-redirects" module made all the difference. Thanks! again. – user5440060 Oct 15 '15 at 05:37