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

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


Take command prompt and type ipconfig, so we will be getting many addresses. From that take IPV4 and copy it to the clipboard. Then paste this address to both the back ends port address and front end port address. eg say IPV4 address is 192.168.1.3 then make you your back end route API similar to this http://192.168.1.3:8080/patients, where 8080 is the port number (never mind). And use the same for the front end to grab the API result.