-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
POST FormData still creates Network Error on Android with 0.27.2 #4800
Comments
Solving the issue that can occur in the ReactNative environment is difficult, as it seems that none of the contributors use RN in their daily work in order to be able to quickly create a test environment to fix the bug. At the same time, reports on the workability of FormData in ReactNative are quite inconsistent. Could you please run this test code on the affected platform and post the log here? import axios from "axios";
import utils from "axios/lib/utils.js";
const form = new FormData();
const prototype = FormData.prototype;
console.log(
JSON.stringify({
VERSION: axios.VERSION,
client: typeof navigator !== "undefined" && navigator.userAgent,
node: typeof process !== "undefined" && process.version,
isFormData: utils.isFormData(form),
toString: String(form),
stringTag: form[Symbol.toStringTag],
constructor: prototype.constructor,
props: Object.getOwnPropertyNames(prototype)
})
); |
Sure: |
Same issue for me too |
@zaubara thanks! It seems we need an additional test since it's not a typical problem with FormData instance detection. function isStandardBrowserEnv() {
let product;
if (typeof navigator !== 'undefined' && (
(product = navigator.product) === 'ReactNative' ||
product === 'NativeScript' ||
product === 'NS')
) {
return false;
}
return typeof window !== 'undefined' && typeof document !== 'undefined';
}
function isFormData(thing) {
const pattern = '[object FormData]';
return thing && (
(typeof FormData === 'function' && thing instanceof FormData) ||
toString.call(thing) === pattern ||
(typeof thing.toString === 'function' && thing.toString() === pattern)
);
}
console.log('product', typeof navigator !== 'undefined' && navigator.product);
console.log('Platform', typeof Platform !== 'undefined' && Platform);
console.log('axios:isStandardBrowserEnv', isStandardBrowserEnv());
console.log('axios:isFormData', isFormData(new FormData()));
((name) => {
try {
const xhr = new XMLHttpRequest();
xhr.open('POST', 'https://httpbin.org/post');
const formdata = new FormData();
const text = 'This is awesome';
formdata.append('title', text);
xhr.onerror = console.warn;
xhr.onloadend = () => {
console.log(`[${name}] ${JSON.parse(xhr.response).form.title}`);
}
xhr.send(formdata);
} catch (err) {
console.warn(err);
}
})('without content type');
((name) => {
try {
const xhr = new XMLHttpRequest();
xhr.open('POST', 'https://httpbin.org/post');
const formdata = new FormData();
const text = 'This is awesome';
formdata.append('title', text);
xhr.setRequestHeader('Content-Type', 'multipart/form-data');
xhr.onerror = console.warn;
xhr.onloadend = () => {
console.log(`[${name}] ${JSON.parse(xhr.response).form.title}`);
}
xhr.send(formdata);
} catch (err) {
console.warn(err);
}
})('with content type') |
Same issue for me too |
Different device, but still getting the same error, so it should be identical:
|
@zaubara do you see any network request in the debugger's Network tab from Axios? We seem to be having this same issue, and we do not see Axios creating any network request in the Network tab, but this may be a limitation of Flipper. But the Network Plugin doesn't show Axios making any requests either, and other network requests do show up there. It seems like Axios isn't even sending the request. Edit: Actually, rebooting our development machine seems to have resolved this for us, but that's rather baffling. |
The issue is that axios is setting a wrong |
@mlazari we explicitly set the |
@slapbox That doesn't change anything since you set the same value as the default. I think you must use Note that if you use |
I was having the same issue. Spent and entire day on it. Rolled back to Axios 0.24.0 and it started working. |
React Native Hey guys, I spent hours on this and the only way I could get it to work was to downgrade to 0.24.0 as @jjspierx mentioned.
Where http is a utility that sets up and calls axios along with the config object parameter shown above. Version: |
This is still happening with |
same issue too FormData { |
I got it to work with |
Still not working with |
I resolve the network error problem setting file like this
info
|
only downgrading to version 0.24.0 works. |
I had already downgraded, but the way I was passing the object caused the error too, so I added the info above if helps. |
Does this still affect versions |
Fixed by add correct content type using the rpm mime pkg. import mime from "mime";
const newImageUri = "file:///" + imageUri.split("file:/").join("");
const formData = new FormData();
formData.append('image', {
uri : newImageUri,
type: mime.getType(newImageUri),
name: newImageUri.split("/").pop()
}); |
This works for me too, thanks! Environment: react: 18.1.0 |
@slapbox I'm on the v1.2.2, and I was facing the same issue until I try the solution of @paruckerr, and finally works! Downgrading to 0.24.0 didn't work. |
I had a similar problem about a year ago and didn't remember how I resolved it. And again I have this problem, last time what I remember, it was okHttp bug on android, does anyone have more info about this? |
FormData still creates Network Error on Android with 0.27.2. I have [AxiosError: Network Error] while uploading a document via react-native-document-picker@8.1.2. Please give me suggestions on how to resolve this. |
Solved my problem after adding headers content-type "multipart/form-data" instead of "x-www-form-urlencoded". Axios version: 1.3.4 |
Yes, this no longer creates an error. When sending FormData, there seem to be intermittent errors when sending (NetworkError, Socket closed) that don't seem to appear on iOS - but hard to say what's the exact issue, they also happen on 0.24. Might be something on my end. |
Thanks for previous answers, it also helps for me:
|
Adding the mime type also worked for me. On iOS seems to not be needed. |
On iOS this was working fine, but on Android it was immediately failing without reaching the server with a "NETWORK ERROR". I did have setup correctly in the headers I didn't want to downgrade anything to fix this, if you are like ME this how I fixed it, my mixing @mateusfg7 and @pnxdxt solutions. There are my relevant libraries: "axios": "^1.4.0",
"expo": "~48.0.11",
"react-native": "0.71.8", And this is the code that I sent to my server: import mime from "mime";
// Upload is the document that you receive from the file-picker library in Expo
const uri = Platform.select({
ios: upload.uri.replace("file://", ""),
android: upload.uri,
});
const file = {
name: upload.name,
type: mime.getType(uri),
uri,
}
After this, it has started working flawlessly without downgrading anything. Cheers! |
This worked with me |
This solved my problem too. thanks very much! |
React Native 0.72.4Network Error on POST-ing FormData in Android disappeared by giving the following headers:
|
Same issue here react-native 0.72.1 |
Rolled back to |
not working any of solution ; ( |
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
It will throw the same error since the Reference -> https://stackoverflow.com/a/72899049/5918233 |
for android you just need to set header
|
I was able to fix the issue with below code const data = {}; //or FormData
const headers = {}; //your default headers
axios({
url: "",
method: "post",
data: data,
headers: {
...headers,
...(data instanceof FormData
? {
"Content-type": "multipart/form-data"
}
: {})
}
}); |
I have to send a request strongly without any |
2024, and I still have this problem, it doesn't happen every time, only sometimes and on Android. As I said, it doesn't happen all the time, only once in a while, I haven't found a pattern for it. I know that the file is filled, I have a log showing it before and in the request in the flipper the formData appears filled. But out of nowhere, sometimes this network error happens, and when it happens, if you try to submit again, it works normally. react native: 0.71.1 console.log("file", file)
const formData = new FormData();
formData.append('file', file);
return api.put<IBaseResponse<IOnboardingResponse>>(
submitEvidenceRoute,
formData,
{
headers: { 'Content-Type': 'multipart/form-data' },
},
); I tried using mimo and the solutions above, but nothing... the worst thing is that it only happens once in a while, so I always have to test a lot to see if it solves it or not... |
You really save my day! |
oh, thanks a lot!
|
did you find any solution, same issue happening for me. |
same issue |
I faced exactly the same issue today and managed to address it by replacing the image type with 'image/jpeg'. Old Code: const data = new FormData();
data.append('image', {
name: image.name,
type: 'image',
uri: image.path,
});
const response = await axios.post('/', data, {
headers: {'content-type': 'multipart/form-data'},
}); New Code: const data = new FormData();
data.append('image', {
name: image.name,
type: 'image/jpeg',
uri: image.path,
});
const response = await axios.post('/', data, {
headers: {'content-type': 'multipart/form-data'},
}); This solved the issue for me. Hope it helps! |
@arashfallahi1989 This is the request body on Android
this is the axios request
I keep getting |
Try update axios version to latest and use fetch as adaptor. It work for me. I have tried 100 times formData post requests in a row without "network error". |
Describe the bug
On Android in Expo 45 / React Native 0.68 with Hermes (and Flipper), POSTing a FormData object fails instantly with a Network Error. Works on iOS. Axios 24 works and seems to be the last reliable version.
To Reproduce
Expected behavior
No network error.
Environment
The text was updated successfully, but these errors were encountered: