node.js - 如何使用node.js httpRequest连接到dashdb

标签 node.js ibm-cloud node-red node-request dashdb

我们如何使用node.js的HTTP/Request模块连接到Dash DB并执行R脚本代码。我可以使用 Node-RED 来完成此操作,但喜欢以编程方式完成此操作。现在我收到此错误:

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.

下面是我的代码:

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

最佳答案

我修改了代码,使其更类似于 node-red http 代码(在测试代码之前,请确保更新您的用户名、密码和 URL 以匹配您的 dashDB 实例):

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

我的结果是这样的:

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"}

关于node.js - 如何使用node.js httpRequest连接到dashdb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33098808/

相关文章:

javascript - 为什么我会收到这个已弃用的警告?! MongoDB

php - 通过 cookie 在 php 和 node.js 之间安全地共享数据

android - IBM Bluemix 在一段时间后断开 MqttAndroidClient

python-2.7 - 如何在 DSX 中提供用户功能/模块

raspberry-pi3 - 如何开机自启动Nodered?

javascript - DNS 查找特定服务器

javascript - Bookshelf.js 设置属性不在数据库中

在 Kubernetes 入口上使用 Gunicorn 的 Flask 产生 502 nginx 错误

javascript - Node-RED/javascript : convert multi line string to single line string - strg. 替换不起作用

node.js - IBM Cloudant 的聚合查询,基本上是 couchDB