11

I want to get use this rest api with authentication. I'm trying including header but not getting any response. it is throwing an output which it generally throw when there is no authentication. can anyone suggest me some solutions. below is my code

var http = require('http');

var optionsget = {
    host : 'localhost', // here only the domain name

    port : 1234,

    path:'/api/rest/xyz',
            headers: {
     'Authorization': 'Basic ' + new Buffer('abc'+ ':' + '1234').toString('base64')
   } ,
    method : 'GET' // do GET

};

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

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

    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);
});
Sau
  • 2,109
  • 6
  • 21
  • 22

2 Answers2

48

The request module will make your life easier. It now includes a Basic Auth as an option so you don't have build the Header yourself.

var request = require('request')
var username = 'fooUsername'
var password = 'fooPassword'
var options = {
  url: 'http://localhost:1234/api/res/xyz',
  auth: {
    user: username,
    password: password
  }
}

request(options, function (err, res, body) {
  if (err) {
    console.dir(err)
    return
  }
  console.dir('headers', res.headers)
  console.dir('status code', res.statusCode)
  console.dir(body)
})

To install request execute npm install -S request

Noah
  • 33,851
  • 5
  • 37
  • 32
  • yes this work for me can you please tell me how do i connect this client program with my UI so that i can print the JSON in the UI – Sau Apr 13 '13 at 22:31
  • When you say client program do you mean the node.js process or client-side javascript? Do you have a sample project that you could put up on github? – Noah Apr 13 '13 at 23:20
  • I have the rest API which is coming from the node.js server now i want to use that api to print the jason in my UI.. How can i Do that – Sau Apr 13 '13 at 23:24
  • Is there any way that the JSOn I'm getting in the command prompt will come in the UI either by javascript or by Jquery or by any means. – Sau Apr 13 '13 at 23:27
  • Dear @Noah, I am also in the same phase of confusion as Sau was few years back. I am also trying to use authentication on my Client-Side NodeJS Web App where every routes to the pages in Web app are to be bound by authentication from the server-side nodejs api. Here's my question in detail. https://stackoverflow.com/questions/45560910/how-to-use-authentication-authorization-from-a-web-app-to-remote-nodejs-api-th – Luzan Baral Aug 11 '17 at 05:30
1

In your comment you ask, "Is there any way that the JSOn I'm getting in the command prompt will come in the UI either by javascript or by Jquery or by any means."

Hey, just return the body to your client:

exports.requestExample = function(req,res){
  request(options, function (err, resp, body) {
    if (err) {
      console.dir(err)
      return;
    }
    // parse method is optional
    return res.send(200, JSON.parse(body));
  });
};
Eric Alberson
  • 1,116
  • 1
  • 11
  • 23
hypervillain
  • 393
  • 1
  • 5
  • 18