A request that works against your https API can fail on a device as soon as its URL starts with http://, because iOS, and Android from API level 28, block cleartext traffic by default. A few habits from web development don't carry over either: there is no CORS between the app and a server, and some fetch options behave differently or not at all. Knowing these rules saves you from chasing a server bug when the platform is what refused the request.

Key terms

  • App Transport Security (ATS): the iOS policy, on by default, that requires HTTP connections to use HTTPS.
  • NSAppTransportSecurity: the Info.plist key that holds an app's ATS exceptions.
  • NSExceptionDomains: the ATS setting for exceptions that apply only to the domains you list.
  • android:usesCleartextTraffic: the manifest attribute that overrides Android's default block on cleartext traffic.

ATS exceptions on iOS

To fetch from a URL that begins with http, add an ATS exception to Info.plist under NSAppTransportSecurity. NSAllowsArbitraryLoads disables ATS globally, and NSExceptionDomains scopes the exception to the domains you name. Prefer the per-domain form, because App Store review requires a justification for disabling ATS.

An app created from the React Native template already has an NSAppTransportSecurity entry in its Info.plist:

xml
<key>NSAppTransportSecurity</key>
<dict>
  <!-- Do not change NSAllowsArbitraryLoads to true, or you will risk app rejection! -->
  <key>NSAllowsArbitraryLoads</key>
  <false/>
  <key>NSAllowsLocalNetworking</key>
  <true/>
</dict>

Leave NSAllowsArbitraryLoads at false and put the exception for your test server's domain in NSExceptionDomains.

Cleartext on Android from API level 28

On Android, cleartext traffic is blocked by default as of API level 28. Setting android:usesCleartextTraffic in the app manifest overrides that. The template's main AndroidManifest.xml reads the attribute from a ${usesCleartextTraffic} build placeholder rather than a fixed value.

Debug builds need cleartext for their own reason, since the debug app talks to the Metro bundler over HTTP. When you add React Native to an existing Android app, the integration guide turns on usesCleartextTraffic in the debug manifest and nowhere else.

No CORS inside a native app

React Native installs its own XMLHttpRequest as a global at startup, and the fetch polyfill is built on it: every fetch call creates an XMLHttpRequest. Because XMLHttpRequest is built in, libraries that depend on it, such as axios, work in an app, and you can call XMLHttpRequest directly if you prefer.

Native apps have no concept of CORS, so the CORS configuration a server keeps for browser clients plays no part when your app calls the same endpoint. That is a different security model from the web's.

Redirects and cookies

redirect: 'manual' does not work with fetch in React Native, and React Native's RequestInit type doesn't declare a redirect option at all. On iOS, React Native follows every redirect itself and loads the stored cookies for the new URL before sending the next request.

Cookie-based authentication with fetch is currently unstable, and one known failure involves redirects. On iOS, when a 302 response carries a Set-Cookie header, the cookie is not set properly. Since you can't handle the redirect manually, a redirect caused by an expired session can turn into an endless series of requests.

The credentials option accepts 'omit', 'same-origin' or 'include'. On iOS, a plain fetch call attaches the cookies held in the shared cookie storage, because React Native's XMLHttpRequest starts with withCredentials set to true and only 'omit' sets it to false. Pass credentials: 'omit' for an API that authenticates with a header, as the fetchProfile helper does. On iOS that covers only the first request: if the server redirects, React Native's redirect handler loads the stored cookies for the redirect target whatever credentials says.

One header rule is Android only: when a request has several headers with the same name, only the last one is kept.

Code Examples

tsx
type Profile = {id: string; displayName: string};

export async function fetchProfile(accessToken: string): Promise<Profile> {
  const response = await fetch('https://api.example.com/me', {
    // 'omit' keeps iOS from attaching stored cookies to this request (a redirect still loads cookies for the new URL)
    credentials: 'omit',
    headers: {Authorization: `Bearer ${accessToken}`},
  });
  if (!response.ok) {
    throw new Error(`Profile request failed with status ${response.status}`);
  }
  return (await response.json()) as Profile;
}
✓ Completed