0

I have a local flask server that has the config flask run --host:0.0.0.0 as specified in the docs. Instead of receiving a response from my flask server when i call it from react-native, I get this error:

- node_modules\react-native\Libraries\vendor\core\whatwg-fetch.js:504:29 in onerror
- node_modules\event-target-shim\lib\event-target.js:172:43 in dispatchEvent
- ... 8 more stack frames from framework internals"

All examples I have found of this problem had people using "localhost", which is not what I am doing, and the troubleshooting steps have not worked. I am using expo, which is handling the 'fetch' function from the below call. This is not running any android or ios specific app yet, as the code is still in the early phases.

react-native call:

export function get_page(){
    url = 'http://<local ipv4 address>:5000/'
    return fetch(url)
    .then((response)=>{
        console.log(response)
        return response
    })
    .catch((error) =>{
        console.log(error)
        return error
    })
}

simple python code:

@app.route('/')
def hello_world():
    return "Hello, World!"

Again, I only receive a network error after some time spent attempting to connect. When I access the URL from firefox or python's IDLE, I can see the page, meaning that it is strictly an issue with my react-native environment.
Any help is appreciated as always.

A.Lopez
  • 53
  • 7
  • maybe see https://stackoverflow.com/questions/51363339/react-native-app-transport-security-has-blocked – Anthony Oct 20 '19 at 00:28

1 Answers1

0

it is the network permission. today, android and ios don not allow HTTP connection default

for Android, you must add networkSecurityConfig in the AndroidMainfest.xml

<application
   ...
 networkSecurityConfig="@xml/networkConfig"
 .../>

 //the config content like below

<network-security-config>
//one way allow all http connection
<base-config cleartextTrafficPermitted="true"/>
// the other way allow some fixed url
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
</domain-config>

</network-security-config>

for ios, you should add the address in the info.plist

<key>address</key>
<dict>
  <key>NSExceptionAllowsInsecuredHTTPLoads</key>
  <true/>
 <key>NSExceptionRequiresForwardSecrecy</key>
 <true/>
 <key>NSIncludesSubdomains</key>
 <true/>
 </dict>
Lenoarod
  • 3,441
  • 14
  • 25