2

I am trying to access opencorporates.com and using their REST API. I got this code from How to make remote REST call inside Node.js? any CURL?. But it is not fetching any data. I tried wget on the url and it worked perfectly fine.

app.js

var https = require('http');

var optionsget = {
host : 'opencorporates.com', 

port : 8080,
path : '/v0.2/companies/search?q=barclays+bank&jurisdiction_code=gb', 
method : 'GET' 
};


console.info('Options prepared:');
console.info(optionsget);
console.info('Do the GET call');

var reqGET = https.get(optionsget, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);


res.on('data', function(d) {
    console.info('GET result:\n');
    process.stdout.write(d);
    console.info('\n\nCall completed');
  });

});

reqGet.end();
reqGet.on('error', function(e) {
console.error(e);
});
Community
  • 1
  • 1
blackmamba
  • 1,952
  • 11
  • 34
  • 59

4 Answers4

2

Try using request module. https://github.com/mikeal/request It's the http module on steroids.

Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
  • Thank you so much for the link. But what exactly is wrong with this code? – blackmamba Dec 11 '13 at 19:58
  • It's very low level, you don't have the onEnd handler and you have a few typos. Request is much powerful and leverages these low level calls. – Silviu Burcea Dec 12 '13 at 07:21
  • 1
    Speaking of steroids, have you heard of https://github.com/simov/purest It's built on top of _request_ and it's designed especially for REST APIs – simo Oct 07 '14 at 05:48
2

Because Node runs asynchronously, the returned data is broken into chunks.

The .on('data') event returns a portion of the data, which you then need to stitch/append back to a variable. You can then capture the complete output with .on('end').

See this example for more info: Why is node.js breaking incoming data into chunks? (@vossad01's answer)

That said, @SilviuBurcea's suggestion to use request is a much simpler way of handling http requests so you don't have to write and handle all of this yourself.

Community
  • 1
  • 1
brandonscript
  • 68,675
  • 32
  • 163
  • 220
1

Tried running the code locally, and first there is a capitalization error

var reqGET = https.get(optionsget, function(res) {

reqGet.end();

Second, the web address was not working at the address, nor with secure

var optionsget = {
host : 'api.opencorporates.com', 
port : 80,
path : '/v0.2/companies/search?q=barclays+bank&jurisdiction_code=gb', 
method : 'GET' 
};

Its worth noting that if you wanted to actually use https, you would need to change the require line

var https = require('https');
gmcerveny
  • 191
  • 1
  • 17
  • I rectified the capitalization error. The web address is working in my browser, it displays a json formatted data. http://api.opencorporates.com/v0.2/companies/search?q=barclays+bank&jurisdiction_code=gb Still the code doesn't return any data. – blackmamba Dec 11 '13 at 20:10
0

This is fully functional version for your reference:

var http = require('http');

var optionsget = {
    host : 'api.opencorporates.com',
    port : 80,
    path : '/v0.2/companies/search?q=barclays+bank&jurisdiction_code=gb',
    method : 'GET'
};


console.info('Options prepared:');
console.info(optionsget);
console.info('Do the GET call');

var reqGet = http.get(optionsget, function(res) {
    console.log("statusCode: ", res.statusCode);
    console.log("headers: ", res.headers);

    buffer='';

    res.on('data', function(d) {
        //console.info('GET result:\n');
        //process.stdout.write(d);
        buffer += d.toString();
        //console.info('\n\nCall completed');
    });

    res.on('end', function() {
        console.info('GET result:\n');
        console.log(buffer);
        console.info('\n\nCall completed');
    });

});
reqGet.on('error', function(e) {
    console.error(e);
});

reqGet.end();
Nitin...
  • 1,274
  • 10
  • 18