lante
lante

Reputation: 7336

node.js http with proxy and domain\user:password authorization

Im trying to access a resource from the web throught a proxy. Unlike this response, I need to set a domain\user and password authentication.

Well, this is my code:

var http = require('http');

var options = {
  host: "proxy.domain.com",
  port: 80,
  path: "http://www.google.com",
  headers: {
    Host: "www.google.com"
  },
  method: 'GET',
  auth: 'domain\\user:password'
};

http.request(options, function (data) {
    console.log('success!', data);
}).on("error", function (e) {
    console.log('error :(', e);
});

The code above is thowing inmediatly the following error:

error :( { [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }

So, no request being sent, I guess.

What is wrong? How can I send a request with the parameters I trying to set?

Thanks in advance

Upvotes: 4

Views: 5696

Answers (2)

Kermit_ice_tea
Kermit_ice_tea

Reputation: 518

Here is an updated version

var.auth = "username" : "password";

http.get({
            host   : var.hostname,
            port   : var.port,
            path   : requestUrl.href,
            headers: {
                Host                 : requestUrl.host,
                'Proxy-Authorization': `Basic ${new Buffer(var.auth).toString('base64')}`,
            }
        }, response => {
//do what you want with response
})

Upvotes: 0

Andrej Sramko
Andrej Sramko

Reputation: 833

this code worked for me (encoding in base64 and setting Proxy-Authorization in headers):

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

var options = {
  host: "proxy",
  port: 8080,
  path: "https://www.google.com",
  headers: {
        Host: "www.google.com",
        "Proxy-Authorization" : auth
     }
};

Upvotes: 4

Related Questions