We're trying to connect to an API (.net core 2.2) from react-native (version 0.59), via localhost, both apparently running on the same IP, different ports, react-native on port 8080, and the API on the 44344, the issue happens the moment we fetch the URL from react-native
We’ve also tested running the URL from Postman and everything seems ok, also from any of the web browser installed (safari/chrome), even we tested the browser INSIDE react-native iOS, and it works.
Any API running outside localhost works perfectly, is the localhost were we failed.
Network request failed.
onerror
whatwg-fetch.js:504:29
dispatchEvent
event-target.js:172:43
setReadyState
XMLHttpRequest.js:580:29
__didCompleteResponse
XMLHttpRequest.js:394:25
emit
EventEmitter.js:190:12
__callFunction
MessageQueue.js:366:47
<unknown>
MessageQueue.js:106:26
__guard
MessageQueue.js:314:10
callFunctionReturnFlushedQueue
MessageQueue.js:105:17
callFunctionReturnFlushedQueue
[native code]:0
Part of the code (function) that fetch the API, very simple (for now)
async Submit(){
//GET METHOD
try {
let response = await fetch('https://192.168.1.56:44344/api/values');
let responseJson = await response.json();
return Alert.alert(JSON.stringify(responseJson));
} catch (error) {
console.error(error);
}
}
Ok first of all, we've tried EVERY possible solution, to connect my react native app to my .net core API rest, both running in localhost, this is the list so far, of the things that we've tried so far, and still no result.
- Localhost
- 127.0.0.1
- Computer IP (network IP not MAC address)
- React Native blank project (from the ground up)
- API .net core blank project (from the ground up)
- Running snack expo + API .net core
- IP forwarding (We can't do that do to our job policies/Not the solution we're looking for)
- http/https
- Different ports
- Android and iOS permissions from react-native
- Same network different IP (this sorta worked, but we don't know exactly why it doesn't work running both react-native and the API in the same IP (localhost))
- 10.0.2.2 (for android)
- Enable CORS on API .net core (but apparently this doesn't work on native apps, only for web)
- Expose the ip through ngrok/serveo (We can't do that do to our job policies/Not the solution we're looking for)
- Frisbee
- Axios
- Websocket (We can't do that do to our job policies/Not the solution we're looking for)
- XMLHttpRequest (status code error 0)
- Firewall/proxy (our network is free from firewalls and proxies)
- Web browser plugins (deactivated and/or uninstalled)
- Cache/cookies
- Docker (We can't do that do to our job policies/Not the solution we're looking for)
- Reboot my Macbook Pro
We expect react native to fetch the API, so we can continue with the office 365 login authentication.
EDIT: I just discovered that fetching the machine IP (this time running windows), with my IP being 192.168.0.9 both on the API and the react native, the fetch result showed me the 10.0.2.2:80 in the header of the JSON response. I suppose it is the "localhost" IP from react native. How am I supposed to fetch an IP from localhost if react native is not letting me to do so?
EDIT: We had to go for plan B this time around, we've made it work with a docker on the API, but I need a solution for this problem. I'm 99% sure the issue is react-native and nothing else.
EDIT: After all these weeks one of my colleges managed to solve it. First of all, we couldn't make the firewall in my Macbook Pro work properly. Second, we solved that and found out our API was having issues. He found out the redirection was on https, and the certifications weren't working properly, so he changed this
"applicationUrl": "http://192.168.0.114:5001;https:192.168.0.114:5001"
to
"applicationUrl": "http://192.168.0.114:5001"
192.168.1.56:80?