react-native: Fetch call without a Content-Type throws a "Network request failed" error on Android

Description

On Android, the following code used to work in React Native 0.61:

const url = 'https://example.com/remote.php/webdav/';

const options = {
	"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<d:propfind xmlns:d=\"DAV:\">\n<d:prop xmlns:oc=\"http://owncloud.org/ns\">\n<d:getlastmodified/><d:resourcetype/>\n</d:prop>\n</d:propfind>",
	"headers":
		{
			"Authorization": "Basic XXXXXXXXXXXXXXXX",
			"Content-Length": "190",
			"Depth": 0,
			"If-None-Match": "JoplinIgnore-89439",
			"User-Agent": "Joplin/1.0"
		},
	"maxRetry": 0,
	"method": "PROPFIND",
	"timeout": 120000,
};

try {
	const response = await fetch(url, options);
	console.info('RESPONSE', response);
} catch (error) {
	console.error(error);
}

But with React Native 0.62 and 0.63 (tested both) it gives the dreaded:

TypeError: Network request failed

The above code can be fixed by setting the content type in the header, so by adding this line it works again:

"Content-Type": "text/xml",

So I think there are three issues:

  1. All other frameworks and libraries I’ve tried support the above call without having to set the Content-Type, and React Native also used to work, so something was changed that makes it mess with the Content-Type. Maybe it “intelligently” detects the type and actually sends garbage in one way or another.

  2. If React Native requires a Content-Type for some reason, it should send back a friendly error message such as “Please set the Content-Type” instead of the generic impossible-to-debug “Network request failed”.

  3. Any such non-standard fetch behaviour should be documented.

React Native version:

System:
    OS: Windows 10 10.0.19041
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 1.60 GB / 15.85 GB
  Binaries:
    Node: 14.13.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.17.3 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 6.14.8 - C:\Program Files\nodejs\npm.CMD
    Watchman: Not Found
  SDKs:
    Android SDK:
      API Levels: 23, 25, 26, 27, 28, 29
      Build Tools: 27.0.3, 28.0.2, 28.0.3, 29.0.2, 29.0.3, 30.0.2
      System Images: android-27 | Google Play Intel x86 Atom, android-28 | Intel x86 Atom_64, android-28 | Google Play Intel x86 Atom, android-29 | Intel x86 Atom_64, android-29 | Google APIs Intel x86 Atom, android-29 | Google APIs Intel x86 Atom_64, android-29 | Google Play Intel x86 Atom
      Android NDK: 21.3.6528147
    Windows SDK: Not Found
  IDEs:
    Android Studio: Version  4.0.0.0 AI-193.6911.18.40.6626763
    Visual Studio: 16.1.29102.190 (Visual Studio Community�2019)
  Languages:
    Java: 1.8.0_262 - C:\Program Files\OpenJDK\openjdk-8u262-b10\bin\javac.EXE
    Python: 2.7.18 - C:\Python27\python.EXE
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1
    react-native: 0.63.3 => 0.63.3
    react-native-windows: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Steps To Reproduce

Run the above code, or any PROPFIND fetch call without a Content-Type header.

Expected Results

That the http call succeed like it did for several years.

Snack, code example, screenshot, or link to a repository:

See above code

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 6
  • Comments: 22 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Is this ever going to be fixed? This bug has been there for a long time now (in several GitHub issues) and still there doesn’t seem to be a good fix/solution for it. Have already tried most of the workarounds/suggestions etc but still not working on Android, iOS works fine.

Hi, did anyone find some solution for it? I cannot sleep because of that. When I use postman for api requests everything works fine, when I switch to android emulator it shows me this message.

I am making http requests with the fetch function and it works correctly in debug environment but when I compile the apk in release mode the fetch function does not work in versions higher than android 10, I used an android 7 physical device and it works without problems.

the react native version is 0.63 android 10 emulator: not working android 10 physical device: not working android 7 device: works

solve my problem by adding these lines in my manifest.xml file <manifest … xmlns:tools=“http://schemas.android.com/tools”> <application … android:usesCleartextTraffic=“true” tools:targetApi=“28” …> … </application>

@laurent22 exactly, it will work with the content type enabled, but it shouldn’t make a difference like you said