axios: Axios post request network error on android
Describe the bug
0
So i’m posting a formdata object with axios to a node.js server. On iOS everything works perfectly, the data get posted and the image uploaded. But on android i’m getting this error. PLEASE NOTE THAT I DID READ THE PREVIOUS ISSUES AND TRIED THE SOLUTION. NOTHING WORKED !
To Reproduce
Code snippet to reproduce, ideally that will work by pasting into something like https://npm.runkit.com/axios, a hosted solution, or a repository that illustrates the issue. If your problem is not reproducible, please file under Support or Usage Question
const handleSubmit = async (listing, { resetForm }) => {
const data = new FormData();
listing.images.forEach((image, index) =>
data.append("images", {
name: `product${Math.floor(Math.random() * 1000)}.`,
uri: image,
})
);
const res = await axios
.post("http://192.168.43.8:5000/products/addProduct", data, {
headers: {
"Content-Type": "multipart/form-data",
},
//tried adding this but didn't work out
transformRequest: (data) => data,
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
// handle error
});
}
}
Please note that on iOS it works without a problem. here’s a screenshot of the error when i used the react native debugger

Expected behavior
FORMDATA OBJECT POSTED TO NODE.JS server
Environment
-
Axios Version 0.27.2
-
Adapter : HTTP
-
Browser : Chrome
-
Browser : Version 103.0.5060.53 (Official Build) (arm64)
-
Node.js : v14.17.5
-
OS: macOS big sur version 11.4
-
expo: “~45.0.0”
-
“react-native”: “0.68.2”,
Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 11
- Comments: 30
Ok, so after a few hours of debugging, I finally found what’s wrong - it’s the
Content-Typerequest header. If you don’t set it explicitly, then the default value is set here toapplication/x-www-form-urlencoded-> https://github.com/axios/axios/blob/v0.27.2/lib/defaults/index.js#L10 and that apparently makes the request fail on Android, but not on iOS. Setting the'Content-Type': 'multipart/form-data'explicitly fixes the error. It was working in 0.26.1 because this code was removing theContent-Typeheader: https://github.com/axios/axios/blob/v0.26.1/lib/adapters/xhr.js#L32Downgrade axios version to 0.24.0
I am also getting the same error in android device & simulator Setting
'Content-Type': 'multipart/form-data'didin’t help. For me the error happens only for post request - with multipartf/orm-data Other GET & POST apis are working correctly. Funny thing is all api works correctly in iOSCan confirm setting
'Content-Type': 'multipart/form-data'makes it work againLike its been mentioned above; Setting the
'Content-Type': 'multipart/form-data'explicitly fixes the error.If it doesn’t, make sure the type on your Blob is a valid MimeType. In my case, I was passing
type: 'image', in iOS, it worked fine, but on android, it didn’t work until I updated it totype: 'image/jpeg'accordingly.@mlazari Thank you!!! ‘Content-Type’: ‘multipart/form-data’ fixed it for me!
I see the same issue starting with version 0.27.0 (Network Error on Android, works fine on iOS). It works fine on both platforms with 0.26.1 (with a workaround for another issue though - https://github.com/axios/axios/issues/4412#issuecomment-1025506556).
For me, it was not related to form data or anything frontend related! On Android only, I could not reach our production server. Axios gave an
ERR_NETWORKerror without more info. This was working on iOS! And dev/staging URLs were working perfectly fine on the same Android device.I asked a friend to execute the same Axios request as in my app in his own Android app and he got the same
ERR_NETWORKerror so this meant it was not an issue specific to my Android app… Then I created the fetch version request of my Axios request. example:You can execute this request from any Javascript capable device to see where it does and does notwork. If it works on other devices, the issue is probably on your app side, if it doesn’t work from anywhere, it’s Backend related. In my case, it didn’t work from anywhere so it was an issue for the Backend dev’s.
I was getting the same error. Spent several days and a lot of time upgrading packages and trying all the solutions from issues and stackoverflow. Finally this was the mistake which I don’t think is mentioned which even in the docs.
If we have a
FormDataobjectIt will throw the same error since the
"state"isundefinedand"address"is an object. ButFormDataaccepts only string or File or Blob. So this was causing the issue for me. Once I replacedundefinedwith""and"address"withJSON.stringify(addressObj)it started working.Reference -> https://stackoverflow.com/a/72899049/5918233
just used fetch over axios working now
EDIT - built the app again with expo eas and again it stops working even with fetch
I have the same problem, but mine only gives an error when I use android 5.1.1