Javascript Proxy Configuration Examples


Proxy Environment Variables

The Requests library recognizes the following environment variables:

  • HTTP_PROXY / http_proxy. The value should look like:http://PROXYHOST:PORT   
  • HTTPS_PROXY / https_proxy. The value should look like:http://PROXYHOST:PORT   

These should contain the hostname and port of the proxy server, and look like the above example. You can also include a username & password, so that the proxy value looks like http://username:password@proxyhost:port   , but this is unnecessary if you are using IP authentication. On paid plans you can also use a bearer token via your client's proxy authorization header. For more details on environment variables with the Requests library, please see Controlling proxy behaviour using environment variables.

Proxy Option

Instead of using environment variables, you can also pass a proxy option to your request. The proxy   option should contain the same kind of values as the environment variables above.

Here is a JavaScript code example using a single external proxy parameter. This sample is from Stackoverflow.

var request = require('request');
request({
  'url':'https://example.com',
  'method':"GET",
  'proxy':'http://PROXYHOST:PORT'
}, function(error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body);
  }
})

More about nodejs

Follow the link for a brief overview of nodejs.

And, if you’re interested in the overall process of nodejs HTTP handling, you can read about it at Node.js. v2 6.1 documentation.

Axios Library

Axios is a library used with nodejs. Popular features include request and response interception and support for progress reporting and for older browsers. Axios is widely used for HTTP requests as an alternative to the Fetch API.

Axios can also make requests over HTTPS. However, there may be support issues affecting HTTPS proxies; for example, use of the GET method may trigger error responses. This Stackoverflow thread offers three possible solutions:

  • Pass the proxy through httpsProxyAgent   using HTTP.
  • Via npm, install axios-https-proxy-fix   . Then import axios from axios-https-proxy-fix   .
  • Explicitly identify the port. (For ProxyMesh, this is 31280   .)

Here is a code example of httpsProxyAgent   which will enable the GET method in an HTTPS request.

const axios = require('axios'); 
const httpsProxyAgent = require('https-proxy-agent');

const httpsAgent = new httpsProxyAgent('http://username:pass@myproxy:port');
// or const httpsAgent = new httpsProxyAgent({ host: 'myproxy', port: 9999 });

const config = {
  url: 'https://google.com',
  httpsAgent
}

axios.request(config).then((res) => console.log(res))

If you’ve previously authorized your IP address, you won’t need a username:password   configuration. If you do keep username:password instead, be sure to include the port 31280.

Further Information

Selenium WebDriver and JavaScript

Selenium is a great tool for automated functional testing on websites in many programming/scripting languages. You can use Selenium and JavaScript together, along with nodejs, to run tests across most popular browsers.

The blog Selenium + JavaScript Best Practices offers tips on combining them for best results, as well as test writing in Selenium.


Downloads and Information


Proxy and auth Settings


So, to use username:password authentication, you must do the following in your code:

page.customHeaders = {'Proxy-Authorization': 'Basic '+btoa('USERNAME:PASSWORD')};

Still need help? Contact Us Contact Us