Monday, October 24, 2016

Calling REST W/S using NodeJS


Follwing code given belwo can be used to call REST W/S using NodeJS
/**
* New node file
*/
var Client = require('node-rest-client').Client;

var client = new Client();

// direct way 
client.get("http://services.groupkt.com/country/get/iso2code/IN", function (data, response) {
// parsed response body as js object 
console.log(data);
// raw response 
console.log(response);
});

// registering remote methods 
client.registerMethod("getIndiaCountryName", "http://services.groupkt.com/country/get/iso2code/IN", "GET");

client.methods.getIndiaCountryName(function (data, response) {
// parsed response body as js object 
console.log("Data----------"+data);
// raw response 
console.log("response----------"+response);
});
//
/*
* console.log(data); will give you belwo output.
{ RestResponse: 
{ messages: 
[ 'More webservices are available at http://www.groupkt.com/post/f2129b88/services.htm',
'Country found matching code [IN].' ],
result: { name: 'India', alpha2_code: 'IN', alpha3_code: 'IND' } } }
*/
Note: Make sure to install node-rest-client module in your application. Command to install the same
npm install node-rest-client

No comments: