tags:

views:

33

answers:

1

hi all,

as title, how to do that?

var http = require('http');
var client = http.createClient(80, 'www.example.com'); // to access this url i need to put basic auth.

var request = client.request('GET', '/',
  {'host': 'www.example.com'});
request.end();
request.on('response', function (response) {
  console.log('STATUS: ' + response.statusCode);
  console.log('HEADERS: ' + JSON.stringify(response.headers));
  response.setEncoding('utf8');
  response.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

sorry if my question is not clear, my english bad ..

Thanks before :)

A: 

You have to set the Authorization field in the header.

It contains the authentication type, Basic in this casee and the username:password combination which gets encoded in Base64:

var username = 'Test';
var password = '123';
var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');

// auth is: 'Basic VGVzdDoxMjM='

var header = {'Host': 'www.example.com', 'Authorization': auth};
var request = client.request('GET', '/', header);
Ivo Wetzel
thanks ... but i dont know why the string need to encode to base64 ...
de_3
That's just how the whole thing works. The server expects the data to be encoded in Base64.
Ivo Wetzel