35

I'm trying to use the module request in my node.js app, and I need to configure proxy settings with authentication.

My settings are something like this:

proxy:{
    host:"proxy.foo.com",
    port:8080,
    user:"proxyuser",
    password:"123"
}

How can i set my proxy configuration when i make a request? Could someone give me an example? thanks

4 Answers 4

54

Here is an example of how to configure (https://github.com/mikeal/request/issues/894):

//...some stuff to get my proxy config (credentials, host and port)
var proxyUrl = "http://" + user + ":" + password + "@" + host + ":" + port;

var proxiedRequest = request.defaults({'proxy': proxyUrl});

proxiedRequest.get("http://foo.bar", function (err, resp, body) {
  ...
})
Sign up to request clarification or add additional context in comments.

1 Comment

There is no response when I do this. Do you know any particular reason?
25

The accepted answer is not wrong, but I wanted to pass along an alternative that satisfied a bit of a different need that I found.

My project in particular has an array of proxies to choose from, not just one. So each time I make a request, it doesn't make much sense to re-set the request.defaults object. Instead, you can just pass it through directly to the request options.

var reqOpts = {
    url: reqUrl, 
    method: "GET", 
    headers: {"Cache-Control" : "no-cache"}, 
    proxy: reqProxy.getProxy()};

reqProxy.getProxy() returns a string to the equivalent of [protocol]://[username]:[pass]@[address]:[port]

Then make the request

request(reqOpts, function(err, response, body){
    //handle your business here
});

Hope this helps someone who is coming along this with the same issue. Cheers.

3 Comments

Is reqProxy another package?
@sidonaldson No, reqProxy is just a module I wrote to serve up the proxy string.
Strangely. request.defaults didn't work for me. Instead this solution worked.
8

the proxy paramater takes a string with the url for your proxy server, in my case the proxy server was at http://127.0.0.1:8888

request({ 
    url: 'http://someurl/api',
    method: 'POST',
    proxy: 'http://127.0.0.1:8888',
    headers: {
        'Content-Length': '2170',
        'Cache-Control': 'max-age=0'
    },
    body: body
  }, function(error, response, body){
    if(error) {
        console.log(error);
    } else {
      console.log(response.statusCode, body);
    }

    res.json({ 
      data: { body: body } 
    })
});

1 Comment

putting an http: in proxy was the key for me.
0

This code from proxyempire.io did work for me

const http = require('http');
const https = require('https');
const username = 'your-user';
const password = 'your-pass';
const auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');

http.request({
  host: 'your-proxy', // DNS or IP address of proxy server 
  port: 8080, // port of proxy server
  method: 'CONNECT',
  path: 'google.com:443', // some destination, add 443 port for https!
  headers: {
    'Proxy-Authorization': auth
  },
}).on('connect', (res, socket) => {
  if (res.statusCode === 200) { // connected to proxy server
    const agent = new https.Agent({ socket });
    https.get({
      host: 'www.google.com',
      path: '/',
      agent,      // cannot use a default agent
    }, (res) => {
      let chunks = []
      res.on('data', chunk => chunks.push(chunk))
      res.on('end', () => {
        console.log('DONE', Buffer.concat(chunks).toString('utf8'))
      })
    })
  }
}).on('error', (err) => {
  console.error('error', err)
}).end();

Comments

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.