question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:18 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
priyeshshah11commented, Apr 21, 2021

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.

1reaction
GrzegorzSzwedcommented, Dec 22, 2022

Hi @devang-p-eq! Thank you for the reply but I am using SSL 😕

The problem is quite interesting because in the browser it works too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native Post Request via Fetch throws Network Request ...
Developing with Windows OS/PHP built-in server/react-native Android on device: ... the code which wasnt working : error:[typeError : network Request failed]
Read more >
Using the Fetch API - MDN Web Docs
The Fetch API provides a JavaScript interface for accessing and manipulating parts of the protocol, such as requests and responses.
Read more >
Networking - React Native
You may want to make a POST request to a REST API, or you may need to fetch a chunk of static content...
Read more >
[Solved]-React Native Android device throws error for Fetch ...
Coding example for the question React Native Android device throws error for Fetch while uploading a json file : TypeError: Network request failed-React ......
Read more >
Fetch throws "Network request failed" in React-Native 0.14
Fetch is throwing a Network request failed error when I try to make a request to a local server in the Android emulator....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found