Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

fetch fails with TypeError: Network request failed in React Native

ErrorTypeError: Network request failed

Short answer

Network request failed means the request never got an HTTP response, so there is no status code to read. In React Native the common causes are plain http URLs that iOS App Transport Security or Android cleartext rules block, and a self-signed development certificate that Android does not trust. Check the URL scheme first.

  • Applies to React Native 0.7x, Expo SDK 5x, Android 9 and newer, iOS 17 and newer
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the exact URL and the full error. If the URL starts with http:// and the failure happens in a release or newer-OS build, start with the first cause. If it is https:// with a certificate you made yourself, jump to the third.

    try {
      const res = await fetch(url);
      console.log(res.status);
    } catch (e) {
      console.error('fetch failed for', url, e);
    }
  2. 2Match what you see, then jump to its fix

    1. 1Fails only on iOS, URL begins with http://iOS blocks a plain http URL (App Transport Security)Fix
    2. 2Fails only on Android 9 (API 28) or newer, URL begins with http://Android blocks cleartext http trafficFix
    3. 3https:// URL with a certificate from your own CA, works in a desktop browser or curl -k, fails on AndroidAndroid does not trust your self-signed development certificateFix

What does the error look like?

  • TypeError: Network request failed
  • Network request failed

Cause 1 of 3

iOS blocks a plain http URL (App Transport Security)

1Check

iOS enforces App Transport Security by default, so an http:// request is refused unless the app declares an exception.

grep -n "NSAppTransportSecurity" ios/*/Info.plist

2Fix

Best fix: serve the API over https. If you must call http, add an exception for that one domain under NSAppTransportSecurity / NSExceptionDomains in Info.plist instead of turning ATS off for the whole app.

<!-- ios/<App>/Info.plist: keep the exception per domain -->
<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <!-- add your host and its insecure-load setting here, as in Apple's ATS docs -->
  </dict>
</dict>

3Verify

Rebuild the iOS app (an Info.plist change needs a native rebuild, not a Metro reload) and repeat the request.

npx react-native run-ios

Confirmed by reactnative.dev

Did this fix it?

Cause 2 of 3

Android blocks cleartext http traffic

1Check

From API level 28, cleartext traffic is blocked by default. Look for usesCleartextTraffic or a network security config in the manifest.

grep -n "usesCleartextTraffic\|networkSecurityConfig" android/app/src/main/AndroidManifest.xml

2Fix

Prefer https. If http is needed for one host, opt in for that domain through a network security config. The app-wide switch android:usesCleartextTraffic also works, but the Android docs advise avoiding broad cleartext permission.

<!-- android/app/src/main/res/xml/network_security_config.xml -->
<network-security-config>
  <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">insecure.example.com</domain>
  </domain-config>
</network-security-config>

<!-- AndroidManifest.xml, on the <application> tag -->
android:networkSecurityConfig="@xml/network_security_config"

3Verify

Rebuild and install the Android app, then repeat the request.

npx react-native run-android

Confirmed by developer.android.com

Did this fix it?

Cause 3 of 3

Android does not trust your self-signed development certificate

1Check

A server with a self-signed certificate or one from a private CA is not trusted by the pre-installed system CAs.

curl -v https://dev.example.com/health

2Fix

Add the CA certificate to res/raw and trust it only in debug builds with debug-overrides. Android applies that block only when the app is debuggable, so a release build is unaffected.

<!-- res/xml/network_security_config.xml -->
<network-security-config>
  <debug-overrides>
    <trust-anchors>
      <certificates src="@raw/debug_cas"/>
    </trust-anchors>
  </debug-overrides>
</network-security-config>

3Verify

Run a debug build and repeat the request. Confirm a release build still uses only the system CAs.

npx react-native run-android --mode debug

Confirmed by developer.android.com

Did this fix it?

How do I stop it coming back?

  • Use https URLs for every environment, including staging, so no cleartext exceptions are needed.
  • Keep any exception per domain, never app-wide.
  • Always wrap fetch in try/catch and log the URL, because errors thrown by fetch are otherwise dropped silently.
  • Test on a real build of both platforms after any Info.plist or manifest change.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.