7

I'm sending an http request over a proxy an need to add username and password to my request. How do I correctly add these values to my options block?

This is my code:

var http = require('http');

var options = {
  port: 8080,
  host: 'my.proxy.net',
  path: '/index',
  headers: {
   Host: "http://example.com"
  }
};


http.get(options, function(res) {
  console.log("StatusCode: " + res.statusCode + " Message: " + res.statusMessage);
});

Currently the response is StatusCode: 307, Message: Authentication Required.

I tried to add username and password to my options but it doesn't work:

var options = {
    port: 8080,
    host: 'my.proxy.net',
    username: 'myusername',
    password: 'mypassword',
    path: '/index',
    headers: {
       Host: "http://example.com"
    }
};

Additional Info: I don't have much information about the proxy, but in another case this authentication method worked:

npm config set proxy http://username:[email protected]:8080
1
  • It really depends on the type of proxy you are going through and the type of authentication scheme it supports, do you have more information about the proxy? Commented Apr 30, 2015 at 17:51

1 Answer 1

5
+250

Ok, this works with my local squid:

var http = require('http');

function buildAuthHeader(user, pass) {
    return 'Basic ' + new Buffer(user + ':' + pass).toString('base64');
}

proxy = 'localhost';
proxy_port = 3128;
host = 'www.example.com';
url = 'http://www.example.com/index.html';
user = 'potato';
pass = 'potato';

var options = {
    port: proxy_port,
    host: proxy,
    path: url,
    headers: {
        Host: host,
       'Proxy-Authorization': buildAuthHeader(user, pass),
    }
};

http.get(options, function(res) {
  console.log("StatusCode: " + res.statusCode + " Message: " + res.statusMessage);
});

Couple notes:

  1. The full URL must be included in the GET line, not just the path, so its not /index.html but http://example.com/index.html
  2. You should also include the host in the host header, so you will have to parse the URL properly
Sign up to request clarification or add additional context in comments.

6 Comments

It is working without the host in the header. Why do I need it?
Well in principle its mandatory, but if it works without, it's a free world!:)
Honestly, if I were you, I'd include it. But that depends on the proxy and it's implementation...
It seems the server isn't reacting to the Host parameter. It's working even if I just write "foo" instead of the correct host...
Well maybe they just ignore it. Its implementation-dependent. As I said, in theory, that's mandatory, and the proxy has to check it, but maybe the guys who wrote it decided to ignore the host header and use the URL instead. Which kind of makes sense because it's redundant, but that's the standard... For example, Firefox the browser includes both, the full URL and the host header, when talking to a proxy.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.