简体   繁体   中英

How to use proxy in npm's request package

In the code bellow I'm trying to use request with a proxy. To check that I use https://v4.ident.me/ to verify.

The documentation does not provide examples so I tried the code bellow but it is printing my actual IP, not the proxie's:

const REQUEST = require('request'); // https://www.npmjs.com/package/request
var r_options = {
    host: '<proxy ip address>', // http://www.freeproxylists.net/
    port: 8080,
    timeout: 30000, // 30s
    headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0'
    },
    url: 'https://v4.ident.me/'

};

REQUEST(r_options, function (error, response, body) {
    console.log(body);
});

So, how to use proxy in npm's request package?

The documentation does not provide any examples on proxy.

Here is how to do it:

const REQUEST = require('request'); // https://www.npmjs.com/package/request

var options = {
 url: 'http://www.somewhere.com',
 proxy: 'http://<myproxy>:<portNumber>'
}

REQUEST(options, function (error, response, body) {

   // your code here

});

Note that to access https websites you need a proxy that supports https .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM