proxy authentication in node.js with module request
Asked Answered
N

4

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

Nickel answered 10/5, 2014 at 19:29 Comment(0)
N
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) {
  ...
})
Nickel answered 25/5, 2014 at 14:45 Comment(1)
There is no response when I do this. Do you know any particular reason?Aruwimi
P
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.

Perihelion answered 17/11, 2014 at 4:24 Comment(3)
Is reqProxy another package?Oestrin
@Oestrin No, reqProxy is just a module I wrote to serve up the proxy string.Perihelion
Strangely. request.defaults didn't work for me. Instead this solution worked.Braun
N
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 } 
    })
});
Nailhead answered 28/10, 2015 at 10:32 Comment(1)
putting an http: in proxy was the key for me.Edmead
E
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();
Evidence answered 20/9, 2023 at 14:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.