axios: Unable to send formData in axios (react-native android)
I am making a POST request to “Cloudinary” server to upload an image and sending formdata using axios in react-native. The whole process is working fine on iOS but on android i am getting “Network Error”.
I am using axios 0.18.0 and stuck on this from last 3 days. Someone please help me.
This issue has been faced by many people but there is no solution so far.
My code:
` var photo = { uri: image.sourceURL, type: image.mime, name: image.filename, };
var formData = new FormData();
formData.append('file',photo);
formData.append('upload_preset','abcde');
axios({
url:'https://api.cloudinary.com/v1_1/abcde/upload',
method:'POST',
headers:{
'Content-Type':'application/x-www-form-urlencoded'
},
data:formData
}).then(function(response){
}).catch((error) =>{
//Network error comes in
}); `
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 35
I had the same issue, Adding
type: 'image/jpeg',
to the file attribute of the formData fixed it.For those who’s struggling in this issue:
After finding a ton of solutions on internet I was still stuck in this problem, some people say it’s about the network problem (android prevent HTTPS,…), some others say we need to add
multipart/form-data
in header along with the request sent to server. I tried all but didn’t workThe thing is when I put the file inside
formData
it breaks, if I comment it and just send for example a number, it’s fine.THE SOLUTION is: on Android you need to specify the file path with
file://
as prefix. So do like this:God bless you! ❤️
Just tried and it is still giving same error - " Error: Network Error".
In the console log i am getting this:
Thanks @cesar3030.
Was a mime type problem.
type: 'image/jpeg'
solved this issue for AndriodWe’ve gotten around this by keeping the
'Content-Type':'application/x-www-form-urlencoded'
headers, but using a string in the formdata format, instead ofFormData
, like:axios in react native uses the nodejs implementation as an adapter and it’s working fine in both android and ios but in android I don’t know why you should add the correct image/file type to your form data or it will raise “Network Error” try this
formData.append('file', {...photo,type:"image/jpeg"});
or pngany update on this? or at least a workaround?
Issue is persistent on react native, issue should be re-opened, none of the above solutions work. I think we need more mature approach in debugging, where can I see exact issue because of which my request is never sent out to server? On emulator i saw something like bad file descriptor, but I usually use real device, that doesn’t show such issue when i inspect
err.request
@vishal2947 I’m stuck at the same step too, could you explain what you have changed with axios to get it to upload the file if it’s not too much to ask? Thank you.
I have already checked out this link but none of the answers seems to work 😐