I'm getting tired of trying to solve this problem on my own. I'm trying to get a stock market api working, and to do that, I need an ssl connection to it. Alas all I get is errors or something weird.
When I to connect to the socket by just using normal node's "net" module like this:
var net = require('net');
var data = '';
var login = '{"cmd":"login", "args":{"session_key":"' + session_key + '", "service":"NEXTAPI"}} \n';
var socket = new net.Socket();
socket.connect(443, 'pub.api.test.nordnet.se', function () {
console.log('Connected');
});
socket.on('data', function (stuff) {
console.log(stuff.toString);
data += stuff;
});
socket.on('end', function () {
console.log(data);
});
socket.on('error', function (e) {
throw e;
});
socket.on('close', function () {
console.log('Connection closed.');
console.log(data.toString);
});
socket.write(login , function () {
console.log("Callback");
});
First everything goes just fine, I get the "Connected" message and I'm happy, but THEN when I try to do the actual logging in, all I get to my console is "[Function]" and then "Connection closed". Yeah it seems that the data which comes from the server is only "[Function]". Which is kinda weird. EDIT: Now I know why I get it, thank you Amberlamps. Though the problem persists.
Okay, so I tried the same thing with a few other ways. Here's my code using socket.io-client.
var client = io_client.connect('https://pub.test.api.nordnet.se:443', {secure: true});
client.on('data', function (stuff) {
console.log (stuff);
});
client.on('error', function (e) {
throw e;
});
client.on('end', function () {
console.log('Connection ended');
});
And all I get is "Error: getaddrinfo ENOENT". And this seems to be the same error whatever I try to do with it, change the host name to skittles or change the https to http or remove it completely or god knows what I've tried.
Okay... here's my FINAL try, I though that maybe, just maybe, they really do need the certification and I have not used it. So here's my yet another piece of code for using node's tls module:
var tls = require ('tls');
var fs = require ('fs');
var options = {
host: 'pub.api.test.nordnet.se',
port: '443',
key: fs.readFileSync('NEXTAPI_TEST_public.pem'),
cert: fs.readFileSync('NEXTAPI_TEST_public.der')
};
var Stream = tls.connect(options, function () {
console.log('client connected', Stream.authorized ? 'authorized' : 'unauthorized');
process.stdin.pipe(Stream);
process.stdin.resume();
});
Stream.on('data', function (stuff) {
console.log(stuff);
});
Stream.on('end', function () {
console.log('END');
});
Stream.on('error', function (err) {
throw err;
});
But it doesn't even get to the certification part, all it gives me is "Error: connect EADDRNOTAVAIL". And once again I've tried to change the name, port, https to hppt etc but all I get is the same error. Oh and you might think that I have just an error in my host name, but no, I get that host name and port from the same api using a different technique, but I need this one working too in order to progress.
Now please, someone give me some insight on this problem. If you find anything which I could improve on any of the three pieces of code, I will try and do that.
EDIT(1): Here's what happens if I try to use just normal https and not sockets.
var https = require ('https');
var options = {
host: 'pub.api.test.nordnet.se',
path: '/',
port: '443',
auth: session_key + ':' + session_key, // Tried with and without this.
headers: {"Accept": "application/json", "Connection": "Keep-Alive"}
};
var request = https.get(options, function (res) {
res.on('data', function (stuff) {
console.log(stuff)
});
res.on('end', function () {
console.log("END");
});
})
.on('error', function (e) {
throw e;
});
All I get is "Error: socket hang up". Or if I specify https://pub.api... I get "Error: getaddrinfo ENOENT".
As this is a REST api, the attempt with the https module is the right one. But, according to the documentation, you should try to connect to "https://api.test.nordnet.se/next/1" instead. '1' being the API version. This should explain the ENOENT / connection reset.
Querying this will return you the api status. The next step is to login.
I found a pretty good Python example here: https://api.test.nordnet.se/boards/2/topics/297?r=340#message-340
There is a fresh Node example at: https://api.test.nordnet.se/projects/api/wiki/Code_examples#Login-and-feed-example-in-Nodejs
You basically need to login using rest and then login to the feeds with the session key.