Search code examples
node.jssquid

Node.js - HTTP get through Squid proxy authorization issue


I am trying to make a simple request using http.get. But I need to make this request through Squid proxy. Here's my code:

var http = require('http');
var username = 'username';
var password = 'password';
var host = "proxy_host";
var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');

var options = {
  host: host,
  port: 3128,
  path: "http://www.google.com",
  authorization: auth,
  headers: {
    Host: 'www.google.com'
  }
};

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
});

req.end();

My username and password is correct according to the ops team that set up the proxy. The problem is that I keep getting a 407 - authorization required status coming back.

Is there something wrong with the way I am making the request? Or does Squid proxy need to be configured?

Thanks in advance.


Solution

  • You should include auth in the headers section of options:

    var options = {
      host: host,
      port: 3128,
      path: "http://www.google.com",
      headers: {
        'Proxy-Authorization': auth,
        Host: 'www.google.com'
      }
    };