Repository navigation
TypeError: Network request failed On Android #25244
Description
Hi Everyone, I found this problem on my React-Native Android App.
When I using a fetch to communicate with a working api, this type of error is returned to me.
TypeError: Network request failed
I Used the Chrome debbuger and I found this:
TypeError: Network request failed at XMLHttpRequest.xhr.onerror (whatwg-fetch.js:504) at XMLHttpRequest.dispatchEvent (event-target.js:172) at XMLHttpRequest.setReadyState (XMLHttpRequest.js:580) at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:394) at XMLHttpRequest.js:507 at RCTDeviceEventEmitter.emit (EventEmitter.js:181) at MessageQueue.__callFunction (MessageQueue.js:366) at MessageQueue.js:106 at MessageQueue.__guard (MessageQueue.js:314) at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:105)
This is the code that I used:
`static creaUtente(identity, FirstName, LastName, FiscalCode , Email) {
let formdata = new FormData();
formdata.append('Identity', identity);
formdata.append('FirstName', FirstName);
formdata.append('LastName', LastName);
formdata.append('FiscalCode', FiscalCode);
formdata.append('Email', Email);
console.log(Configuration.base_url.rest + Configuration.apiRoutes.signUp)
console.log(formdata)
return new Promise((resolve, reject)=> {
fetch('https://linktotheapi.com',{
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
},
body: formdata
})
.then((response) => response.json())
.then((responseData) => {
if(responseData.Error){
Alert.alert("Errore");
}
global.utente = responseData;
resolve(responseData)
})
.catch((err) => {reject(err)})
})
}`
I manually tried to use the API and it works. So the problem is fetch.
I have seen and read that a lot have encountered this problem on android without a real solution.
The API link is a link type: https: //****.com (it is not a localhost).
I tried to use the http version of the API but the error still appears.
Now, This is my package.json
dependencies": { "@react-native-community/async-storage": "^1.4.2", "buffer": "^5.2.1", "pouchdb-adapter-asyncstorage": "^6.4.1", "pouchdb-authentication": "^1.1.3", "pouchdb-find": "^7.0.0", "pouchdb-react-native": "^6.4.1", "react": "16.8.3", "react-native": "0.59.4", "react-native-ble-manager": "^6.6.2", "react-native-ble-plx": "^1.0.3", "react-native-json-tree": "^1.2.0", "react-native-keyboard-aware-scroll-view": "^0.8.0", "react-native-router-flux": "^4.0.6", "react-native-tab-navigator": "^0.3.4", "react-native-vector-icons": "^6.4.2" }, "devDependencies": { "@babel/core": "7.4.3", "@babel/runtime": "7.4.3", "babel-jest": "24.7.1", "jest": "24.7.1", "metro-react-native-babel-preset": "0.53.1", "react-test-renderer": "16.8.3" }, "jest": { "preset": "react-native" } }
I have read that this problem is on Android Device ( I obviously added internet permissions in the Manifest file ). Do you know nothing about this problem??
Activity
react-native-bot commentedon Jun 12, 2019
Thanks for submitting your issue. Can you take another look at your description and make sure the issue template has been filled in its entirety?
👉 Click here if you want to take another look at the Bug Report issue template.
lucky-newbie commentedon Jun 13, 2019
did you solve it? i met too...
keyz23 commentedon Jun 13, 2019
No I haven't solve it...
keyz23 commentedon Jun 14, 2019
Nobody?
Valiums commentedon Jun 20, 2019
hey @keyz23 @823844596,
I recently encountered something similar using fetch and formDatas (only on Android though).
The workaround that worked for me was to use XMLHttpRequest instead of fetch.
Hopefully this will help you too!
I don't know if you are using expo but for me this problem started when I updated my expo SDK from 31 to 33 (so RN 0.57.1 to 0.59.8)
amohit1315 commentedon Jun 21, 2019
Was anyone able to solve it??
I am facing similar issue with fetch. I have to use common code for android and iOS. fetch works great in iOS but in android throws error.
Trying to fetch data from remote server via api.
Using fetch(https:url:port) to fetch the data. But every time android emulator as well as physical device gives network request failed. While I can access same url within browser of emulator.
I have already went through various links, and many people have posted same problem but none of them had ample solution that works. While some people were not able to access localhost via api-call but it was resolved using IP address of system. But in my scenario I am trying to access remote host. Also domain name cannot be used in my scenario, so need a solution using (ip) in fetch method=get.
testlogin = https://10.209.7.115:8080/api/v1/
Basicauth = 'Basic cm9vdDpyb290'
fetch(testingLogin, {
method: "GET",
cache: "no-cache",
mode: "cors",
withCredentials: true,
credentials: "include",
headers: {
'Authorization':Basicauth,
Accept: "application/json",
"Content-Type": "application/json"
}
}).then(response =>
{
if(response.status === 200)
{
this.props.navigation.navigate('Home');
}
else
{
alert("Authorization failed");
}
})
.catch(error => {
alert("Request Failed :Check the ip address Entered:Problem with server ");
throw(error);
Error:
TypeError: Network request failed
onerror@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:24051:31
dispatchEvent@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:28695:31
setReadyState@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:28448:33
__didCompleteResponse@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:28275:29
emit@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:3280:42
__callFunction@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:2577:49
http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:2334:31
__guard@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:2531:15
callFunctionReturnFlushedQueue@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:2333:21
garrinmf commentedon Aug 12, 2019
@keyz23 you closed the ticket, but did you find a fix for it?
IcebergRage commentedon Aug 28, 2019
@keyz23 @garrinmf do you find a solution ?
garrinmf commentedon Aug 28, 2019
@IcebergRage, I got past this, forgot I'd commented anywhere though and it's a bit fuzzy now.
I believe in my case it was failing when I only had the header for
Content-Type: "multipart/form-data"as that was enough to make it work on iOS. But, on Android, I also had to specify that I wanted JSON back with the headerAccept: "application/json". Simply specified both and 💥.ailton-moreira commentedon Aug 31, 2019
I have the same issue. Did any one was able to solve it?
91 remaining items
tuoxiansp commentedon Mar 26, 2022
For me, it was solved in a quite simple way:
->
sedhha commentedon Jun 6, 2022
Well to summarize it all, this definitely is not a problem with React Native. I have tested it well with all the scenarios, here are the things you might consider when you get this error:
If you carefully examine above three, you would automatically find out what's missing, if you do the above in order, I am very sure you should be able to sort it out. Tested it with React 0.64.3
flyskywhy commentedon Jun 20, 2022
fixed
TypeError: Network request failedwhen upload file to http not https with Android debug buildsvshy108 commentedon Jul 25, 2022
For me, I appended
file://to android uri then the issue is solved while iOS does not need the append offile://rychel commentedon Aug 26, 2022
after this, if the problem persist don't forget disabled firewall
React Native sync for revisions c28f313...0cac4d5
samad324 commentedon Oct 24, 2022
I'm still getting this error even after adding
android:usesCleartextTraffic="true"inAndroidManifest.xmlfile.so i added my domain name in
network_security_config.xmlfile.don't forget to add
android:networkSecurityConfig="@xml/network_security_config"in yourAndroidManifest.xmlfile.vladimirevstratov commentedon Jan 10, 2023
In my case it was an issue with expired TLS certificate of domain, found it by checking TLS status of domain.
SpaceSimon commentedon Jan 24, 2023
I have a "Network request failed", only on Android and only with DELETE requests, both in development and production.
The deletion works fine but the request fails so I cannot update the UI.
I have this in my headers (among other things):
RN version is 0.63.5
shahzaib803 commentedon Feb 23, 2023
That solution worked for me .
I believe in my case it was failing when I only had the header for Content-Type: "multipart/form-data" as that was enough to make it work on iOS. But, on Android, I also had to specify that I wanted JSON back with the header Accept: "application/JSON". Simply specified both and 💥.
fvalles commentedon Jun 7, 2023
To anyone still having this issue: I faced the same problem when trying to connect my react native app (android simulator & iOS physical device) to a local BFF.
The solution was not using the URL as
http://localhost:PORTorhttp://127.0.01:PORT, but to use the real IPv4 IP address of my laptop which can be found on macOS navigating to:System settings -> Network -> WiFi (in my case) -> Details -> TCP/IPGeorgeFlorian commentedon Sep 25, 2023
This worked for me on EXPO SDK 49:
I've installed
expo-build-propertiesusingnpx expo install expo-build-propertiesand then added it as a plugin inapp.json:03balogun commentedon Nov 24, 2023
Here's how I resolved this.
The server expects
multipart/form-datafor file uploads, but it seems that the request is being sent with application/x-www-form-urlencoded instead. This discrepancy causes the server to reject the request on Android, it works fine on iOS.The native error looks like this
Explicitly setting the content type via the header fixed the issue.
Adding
multipart/form-datato the request header fixed the issue for me.Buddhaa97 commentedon Dec 11, 2023
@03balogun worked for me. Thanks