title: node js HTTP PROXY request error tunneling socket could not be established statuscode=403
body: <p>I have a node application. I try to connect to a XML API through a proxy. I need a proxy because I host my application on Heroku and the IP addresses change all the time due to load balancing occurring on the Amazon AWS server. The Proxy needs to be unlocked by IT firm in order to have access to XML API. They guaranteed the IP entry is made and the IP is unlocked.</p>
  
  <p>The following code snippet creates the request:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>   async send(method, url, options){
          const reqOptions = {
              headers: {},
              ...options,
              url,
              method,
          };
          //use system proxy
          if( Environment.HTTP_PROXY ){
              reqOptions.proxy = this.proxyUrl;
          }
          if( method === 'POST' || method === 'PUT' ){
              reqOptions.body = options.data;
              if( !reqOptions.headers['Content-Type'] ){
                  reqOptions.headers['Content-Type'] = 'application/xml';
              }
          }
  
          return new Promise( (resolve, reject) =&gt; {
              const req = request(reqOptions, function(error, response, body) {
                  if( error ){
                      console.log("Got error: " + error);
                      reject();
                  }else{
                      resolve(body);
                  }
               });
               if( this.username &amp;&amp; this.password ){
                  req.auth(this.username, this.password);
               }
          });
      }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>Error: tunneling socket could not be established, statusCode=403</p>
  </blockquote>
  
  <p>The reqOptions:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>proxy:
     Url {
       protocol: 'http:',
       slashes: true,
       auth: null,
       host: '--.---.--.---:--',
       port: '--',
       hostname: '--.---.--.---',
       hash: null,
       search: null,
       query: null,
       pathname: '/',
       path: '/',
       href: 'http://--.---.--.---:--/' },
    tunnel: true,
  agent:
     TunnelingAgent {
   options:
    { proxy: [Object],
      headers: [Object],
      ca: undefined,
      cert: undefined,
      key: undefined,
      passphrase: undefined,
      pfx: undefined,
      ciphers: undefined,
      rejectUnauthorized: undefined,
      secureOptions: undefined,
      secureProtocol: undefined },
   proxyOptions:
    { host: '--.---.--.---',
      port: --,
      proxyAuth: null,
      headers: [Object] },
   maxSockets: Infinity,
   requests: [],
   sockets: [ {} ],
   _events: [Object: null prototype] { free: [Function: onFree] },
   _eventsCount: 1,
   request: [Function: request],
   createSocket: [Function: createSecureSocket],
   defaultPort: --- },
    setHost: true,
    originalCookieHeader: undefined,
    _disableCookies: true,
    _jar: undefined,
    port: '----',
    host: '--.---.---.---',
    path:
     '/v8/fidelioiiswrapper.dll/fidelioxmlinterface.datahandler?ic=XX',
    httpModule:
     { Agent: [Function: Agent],
   globalAgent:
    Agent {
      _events: [Object],
      _eventsCount: 1,
      _maxListeners: undefined,
      defaultPort: 443,
      protocol: 'https:',
      options: [Object],
      requests: {},
      sockets: {},
      freeSockets: {},
      keepAliveMsecs: 1000,
      keepAlive: false,
      maxSockets: Infinity,
      maxFreeSockets: 256,
      maxCachedSessions: 100,
      _sessionCache: [Object] },
   Server: [Function: Server],
   createServer: [Function: createServer],
   get: [Function],
   request: [Function] },
    _started: true,</code></pre>
  </div>
  </div>
   </p>
  
  <p>If you need more information to help me I am happy to edit.</p>
  
  <p>Thanks for your efforts.</p>