React Native Fetch API

raw JSON →
3.0.0 verified Sat Apr 25 auth: no javascript

A fetch API polyfill for React Native built on top of the native Networking API instead of XMLHttpRequest, enabling text streaming support. Current stable version is 3.0.0 (2022-12-01). This package is maintained by the React Native community and released on an as-needed basis. Key differentiators: supports streaming text via ReadableStream (Response.body) for performance, avoids XHR overhead. Limited to text-only streaming; binary transfers do not support incremental reading. For apps requiring text streaming in React Native.

error AbortController is not defined
cause react-native-fetch-api does not ship its own AbortController; you need a polyfill.
fix
Install abort-controller: npm install abort-controller, then import AbortController from 'abort-controller'.
error Response is not a constructor
cause Importing Response as default instead of named export.
fix
Use import { Response } from 'react-native-fetch-api' instead of import Response from ...
error TypeError: Failed to construct 'Response': The provided value is not of type '(ResponseInit or...)'
cause Using deprecated positional arguments instead of init object in v2+.
fix
Construct Response with an init object: new Response(body, { status: 200 })
error Network request failed (or similar native error)
cause Possibly using a non-http/https URL or missing permissions in iOS/Android.
fix
Ensure URLs start with http:// or https://. For Android, check AndroidManifest.xml for INTERNET permission.
breaking Response NOW returns the instance directly instead of a Promise
fix Remove any await on Response construction: const response = new Response(body, init); (not await new Response(...))
breaking AbortController is NOT included in this package; must use a polyfill
fix Install and import 'abort-controller' polyfill (e.g., from npm) and use AbortController from there.
gotcha Streaming only works for text response types; binary data is collected as a single chunk
fix Use responseType: 'text' (default) for streaming. For binary, use blob or base64 – note that streaming is not supported.
deprecated The 'text' parameter in Response constructor is deprecated; use init object instead
fix Use new Response(body, { status, statusText, headers }) instead of new Response(body, status, statusText, headers)
gotcha fetch() does not support custom protocols (e.g., file://) – only http/https
fix Use a different method for non-HTTP URLs (e.g., react-native-fs for local files)
npm install react-native-fetch-api
yarn add react-native-fetch-api
pnpm add react-native-fetch-api

Demonstrates using the fetch API with text streaming: importing default fetch and named constructors, making a GET request, and reading the response body as a stream.

import fetch, { Headers, Request, Response } from 'react-native-fetch-api';

async function streamText() {
  const response = await fetch('https://example.com/data', {
    method: 'GET',
    headers: new Headers({ 'Accept': 'text/plain' })
  });
  const reader = response.body.getReader();
  const decoder = new TextDecoder();
  let result = '';
  while (true) {
    const { done, value } = await reader.read();
    if (done) break;
    result += decoder.decode(value, { stream: true });
  }
  console.log(result);
}
streamText().catch(console.error);