nx icon indicating copy to clipboard operation
nx copied to clipboard

react-native localhost fetching api not working

Open victorrik opened this issue 3 years ago • 0 comments

i use the steps for react native applications and try to use axios to make the petitions on my localhost server and always throws error connections and i using the ip of my computer

i'm using firebase functions emulator for the backend

this is how my functions looks carbon

And this is de code that i use to make de petition in both applications carbon (1)

this is what i got and expected on the application with npx react-native init AwesomeProject Screen Shot 2022-08-05 at 18 39 25

this is what i got with the application with nx Screen Shot 2022-08-05 at 18 38 09

Computer: Mac mini M1 Mobile: Samsung Galaxy S10e Dependecies : @nrwl/next: 14.5.2 @nrwl/react-native: 14.5.2 @nrwl/workspace: 14.5.2 @react-native-firebase/app": ^15.2.0 @react-native-firebase/functions": ^15.2.0 axios": ^0.27.2

victorrik avatar Aug 05 '22 23:08 victorrik

@victorrik i think it might be related to this issue: https://stackoverflow.com/questions/38418998/react-native-fetch-network-request-failed.

you need to add your api url to Info.plist and add request URL to NSExceptionDomains under NSAppTransportSecurity for ios, like:

<key>NSAppTransportSecurity</key>  
 <dict>  
  <key>NSExceptionDomains</key>  
  <dict>  
   <key>localhost</key>  
   <dict>  
    <key>NSExceptionAllowsInsecureHTTPLoads</key>  
    <true/>  
   </dict>  
   <key>192.168.100.37</key>  
   <dict>  
    <key>NSExceptionAllowsInsecureHTTPLoads</key>  
    <true/>  
   </dict>
  </dict>  
 </dict>

for android, you might need to change /android/app/src/main/res/xml/network_security_config.xml, and add the request URL to this config file:

<?xml version="1.0" encoding="utf-8"?>  
<network-security-config>  
    <domain-config cleartextTrafficPermitted="true">  
        <domain includeSubdomains="true">10.0.2.2</domain>  
        <domain includeSubdomains="true">localhost</domain>  
        <domain includeSubdomains="true">192.168.100.37</domain>  
    </domain-config>  
</network-security-config>

closing this issue since it is not a bug.

xiongemi avatar Aug 17 '22 18:08 xiongemi