How to connect to WSO2 BAM with NodeJs using REST
Recently I wanted to use WSO2 BAM REST
API to with NodeJs. After playing sometime with NodeJs API I was able
to POST a stream definition to BAM using NodeJs with following
method.
var https = require('https');
var auth = "Basic " + new Buffer('admin:admin').toString("base64"); //You need to replace admin:admin with your username and password
// prepare the header
var postheaders = {
'Content-Type': 'application/json',
'Accept': 'application/json',
"Authorization": auth
};
// the post options
var optionspost = {
host: '127.0.0.1',
port: '9443',
path: '/datareceiver/1.0.0/streams/',
method: 'POST',
rejectUnauthorized: 'false',
headers: postheaders
};
doPOSTRequest=function(optionspost,jsonObject){
// do the POST call
var reqPost = https.request(optionspost, function(res) {
console.log("statusCode: ", res.statusCode);
// uncomment it for header details
// console.log("headers: ", res.headers);
res.on('data', function(d) {
console.info('POST result:\n');
process.stdout.write(d);
console.info('\n\nPOST completed');
});
});
// write the json data
reqPost.write(jsonObject);
reqPost.end();
reqPost.on('error', function(e) {
console.error(e);
});
}
An important ramark is you need to put rejectUnauthorized: 'false' if you are using NodeJs default Cas. Otherwise you may get an error saying [Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE]. This is because NodeJs rejects the certificate provided by BAM in default configuration. This tweak can be applied to any situation where you get the [Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE] when try to send a https request using NodeJs.
The main idea of Node.js framework is use of non-blocking and event-driven I/O to remain insubstantial and efficient in the face of data-intensive real-time applications which run across distributed devices.
ReplyDelete