node.jsaxioseconnrefusedhttps-proxy-agent

Axios Unable to make request to IP address (Error: connect ECONNREFUSED)


I am trying to make a request to a 3rd party SOAP API, provided an IP address http://XXX.XXX.XXX.XXX:40871

I get the following error:

{
    "success": false,
    "error": "connect ECONNREFUSED XXX.XXX.XXX.XXX:40871",
    "trace": "Error: connect ECONNREFUSED XXX.XXX.XXX.XXX:40871\n    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16)"
}

while testing locally I edited my hosts file and provided a host name to the IP

XXX.XXX.XXX.XXX my-host-name.com

then made a request to http://my-host-name.com:40871 and successfully got a response.

I can not do this in production since we are using heroku to deploy our app.

Our app is a middleware to make requests to the 3rd party API since they requires a static IP in its whitelist. we create a axios instance and recieve further options in the request body.

const agent = new HttpsProxyAgent(process.env.STATIC_URL!);
const baseOptions: AxiosRequestConfig = {
    proxy: false,
    httpsAgent: agent,
};
const instance = axios.create(baseOptions);
const response = await instance(req.body);

res.send({ success: true, response: response.data });

here is the request body:

{
    "method": "POST",
    "baseURL": "http://XXX.XXX.XXX.XXX:40871",
    "url": "/IDORequestService/IDOWebService.asmx",
    "data": {{xmlBody}},
    "headers": {
      "Content-type": "text/xml",
      "SOAPAction": "http://frontstep.com/IDOWebService/CreateSessionToken",
      "Accept": "*/*",
      "Accept-Encoding": "gzip, deflate, br",
      "Connection": "keep-alive"
  }
}

Expected behavior: it should work with IP addresses

Environment:


Solution

  • The problem was I did not have an httpAgent so it was not using the proxy for http requests, which is why the API server was probably refusing connection. Since it has the static IP in its whitelist and not our server IP.

    const agent = new HttpsProxyAgent(process.env.STATIC_URL!);
    const baseOptions: AxiosRequestConfig = {
        proxy: false,
        httpsAgent: agent,
        httpAgent: agent,
    };
    const instance = axios.create(baseOptions);
    const response = await instance(req.body);
    
    res.send({ success: true, response: response.data });
    

    adding the httpAgent fixed the issue.