Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - HTTP get through Squid proxy authorization issue

Tags:

node.js

squid

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.

like image 915
ant Avatar asked Feb 23 '23 13:02

ant


1 Answers

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'
  }
};
like image 97
wulong Avatar answered Feb 26 '23 21:02

wulong