-2

The following function calls Rest Web Service and gets some value, but while returning the result does not contain any value.

var http = require('http'); var options = { host : 'localhost', // here only the domain name http://localhost:8080/RestService/rest/message/getItems // (no http/http !) port : 8080, path : '/RestService/rest/message/getItems', // the rest of the url with parameters if needed method : 'GET' // do GET };

function getService() {

http.request(options, function(res) {

  console.log('STATUS1: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
        return chunk;
  });
}).end();

}; console.log(" Return Value --> "+getService());

1 Answers1

-1

Use following way:

http.request(url, function(res) {
        var data = "";
        res.on('data', function (chunk) {
          data += chunk;
        });
        res.on("end", function() {
          return data
        });
      }).on("error", function(e) {
        console.log("Got error: " + e.message);
         return e;
      });
Njain
  • 46
  • 5