Home > Enterprise >  Axios GET request returning ECONNRESET for some URLs
Axios GET request returning ECONNRESET for some URLs

Time:04-06

I am working on an application that uses an express server to reach out to an API to fetch data. In our organisation outbound traffic requires a proxy which I have supplier to axios like below (not the real one):

let response = await axios.get(endpointUrl, {
    proxy: {
        host: "123.45.678.90",
        port: 0000,
    },
})

Passing various URLs into the axios get function returns varied results, with the following URLs returning a result:

Whereas the following URLs are returning an ECONNRESET error almost instantly:

I can't see any pattern between the URLs that are/are not working so wondered if a fresh set of eyes could spot the trait in them? It's important to note that all these URLs return successfully in the browser, just through this axios call being the problem.

To add to the mystery, the URLs that do work work on my machine, do work on a machine outside our organisation - so potentially a clue there?

Any help/guidance of course would be appreciated, thank you.

CodePudding user response:

This error simply means that the other party closed the connection in a way that was probably not normal (or perhaps in a hurry).

For example, a socket connection may be closed by the other party abruptly for various reasons or you may have lost your wifi signal while running your application. You will then see this error/exception on your end.

What could also be the case: at random times, the other side is overloaded and simply kills the connection as a result. If that's the case, depends on what you're connecting to exactly…

Solution - This is happening because you are not listening to/handling the 'error' event. To fix this, you need to implement a listener that can handle such errors.

CodePudding user response:

If the URL that work on your machine also work outside your organization and the other don't, it is most likely a problem with your proxy.

Some proxies might have configurations that makes them remove headers or change the request in a way that the target does not receive it as intended.

I also encountered a problem with axios and proxies once. I had to switch libs to make it work. To be sure, I would recommand using a lib like "request" (deprecated) juste to make sure it is not a problem with axios. There are multiple open issues on the axios repository for proxy issues.

  • Related