javascriptreact-nativeasp.net-corenetwork-programming

How to fix network error in react-native when access localhost API


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.

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"

Solution

  • I got the same issue while fetching localhost API from react-native. Here is what I did to solve this issue. In the startups class, I removed //app.UseHttpsRedirection(); from Configure method.(Not sure if it is needed)

    After that in the launchsetting.js file under the properties folder, I changed from

    "applicationUrl": "https://localhost:5001;http://localhost:5000"

    to "applicationUrl": "http://localhost:5000"

    and in the React native part, I simply changed my localhost with my IP:

    fetch('http://localhost:5000/values/') to fetch('http://127.0.0.1:5000/values/')

    This completely worked for me