axios: Axios returns a generic "Network Error" even when the request got a response code
Describe the bug
When making a request to my API, the catch block has a generic “Network Error” message, but when checking in the network inspector the request got a response code 413. This causes my users to incorrectly report that ‘the website thinks i dont have a solid internet connection’. I suspect this happens for other codes too because this has been going on for a while.
To Reproduce
Make a request with a very large payload to trigger a 413 response
Expected behavior
The catch block should have the response code
Environment
- Axios Version: 0.25.0 and 0.21.2
- Adapter: xhr (i think? i dont remember changing this, so i guess whatever is default?)
- Browser: Chrome
- Browser Version: 97.0.4692.99
- Node.js Version: 14.18.1
- OS: OSX 12.1
Additional context/Screenshots
axiosInstance
.post("/results/add", {
result: completedEvent, //this is very big
})
.then((response) => {
// handle response
})
.catch((e) => {
console.log(e);
//this console logs Error: Network Error
// at createError (monkeytype.js:formatted:35086:25)
// at XMLHttpRequest.handleError (monkeytype.js:formatted:34457:28)
});
My axios instance code: https://github.com/Miodec/monkeytype/blob/master/src/js/axios-instance.js
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 10
- Comments: 49
In my case, the CORS was blocking the frontend from acessing the backend. My Solution: Add this in the server-side:
Hello to Everyone. I just want to let you know that after searching for a solution for two days, I was able to solve my error. Since the proxy was the source of the issue, I must configure a proxy in the package.json file, and I had to follow these instructions in the function that used Axios:
try { await axios.post("user/login", formData).then((res) => { console.log(res.data); }); } catch (error) { console.log(error.response.data.message); }
and in package.json file need to add a proxy:"proxy": "http://localhost:<SERVER_PORT_NUMBER>"
for better understand check this documentation https://www.jamesqquick.com/blog/configure-proxy-in-react/Axios returns a “Network Error” so, the one of the solution. I was found in my project, when using our app we need to switch on the mobile data then, try it again it will clear the ‘network error’.
any solution?
it’s CORS problem, once you resolve the CORS issue (for example install CORS Unblock extension plug in Chrome) it will retrieve the response code (for me it was 504)
@manugch @Miodec do you have any DNS ad blockers? I disabled my AdGuard and now it works fine. Can you check any any blockers/vpn?
So, why does the console correctly show errors like
ERR_FAILED 413
in this case, orCONNECTION_REFUSED
orSSL_CERT_DATE_INVALID
that I ran into in other cases? All of them giveNetwork Error
, but somehow show up correctly in the console…Pretty sure using HTTP is not the best solution as its not supported in every browser (webpack replaces it with xhr)
EDIT: Right… Im assuming its related to the
// Real errors are hidden from us by the browser
comment…Same issue. Any news? issue is coming from the only mobile browsers
I faced the same issue even after installing cors. I was able to fix it by making sure the cors code was above my route declaration. Code below. Before:
After:
Hope this helps
Anyone got the solution to resolve it??
https://github.com/axios/axios/issues/4420#issue-1111893293
Probably the original issue posted here has to do with how CORS is handled by the browser. Looking at your network console the API endpoint is allowing your origin to make the request since OPTIONS request is successful and (taking a guess here) they are also succeeding with small file POSTS and not the bigger than
threshold size
ones.That leads me to think that the content too large 413 is probably served by (
error
response(s) originating from Nginx. If yes chrome is probably disallowing the response from being read by your app. You can resolve this by providingAccess-Control-Allow-Origin
header as a part of the 413 response after which axios will receive the response ( you will need to have access to the reverse proxy or the likes in the middle to add this header).OR
does not
set the ‘Access-Control-Allow-Origin’ headers appropriately while providing theerror response
. If not present chrome is probably disallowing the response from being read by your app (The browser/chrome receives the entire response as can be seen from the network console).-I also see this error on some request:
I had exactly the same issue. Front-end I was using axios and back-end I was using .net core WebAPI. The solution was at the side of the back-end:
app.UseCors((g)=>g.AllowAnyOrigin());
No Adblockers. I also see the error happening on visitor devices in our sentry logs, over 112 iOS devices by now who crashed with that unhandled “Network Error”. It affects only iOS devices as I said before Mac, iPhone, iPad mostly iOS/Safari 14.1.X