2

This code works perfectly fine for a few months:

try {
    return await fetch(url + '/api/v2/login?'+param).then((response) => {
        return response.json()
    });
}
catch (err) {
    console.log(err.message)
}

But last week, react-native throws an error "Network Request Failed". I'm sure that our API is working because I already have tested it in a few online API Tester.

And when I didn't force the HTTPS, and just the HTTP, it works.

And our current react version is: 0.41

Here is the error:

at XMLHttpRequest.xhr.onerror (fetch.js:441)
at XMLHttpRequest.dispatchEvent (event-target.js:172)
at XMLHttpRequest.setReadyState (XMLHttpRequest.js:546)
at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:381)
at XMLHttpRequest.js:485
at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
at MessageQueue.__callFunction (MessageQueue.js:266)
at MessageQueue.js:103
at MessageQueue.__guard (MessageQueue.js:231)
at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:102)
Ace
  • 29
  • 1
  • 1
  • 6
  • Which platform did this problem occurred? – Rex Low Jan 09 '18 at 13:27
  • In Android. Also, I'm fetching from our cloud server, no changes on both sides. The error just suddenly occur. – Ace Jan 09 '18 at 13:31
  • Have you added Internet permission in the AndroidManifest.xml file? – Rex Low Jan 09 '18 at 13:34
  • Last thing, I have created a latest react version app and test this code, and it works! So, Does it have something to do with the react-native version ? – Ace Jan 09 '18 at 13:34
  • It is possible, by the way, what android version are you on? Because Android 7.0 has a different configuration – Rex Low Jan 09 '18 at 13:35
  • Yes, it is included in the AndroidManifest.xml – Ace Jan 09 '18 at 13:36
  • I have tested it in an emulator (marshmallow), real android phone (Nougat). And additionally, when I didn't force the HTTPS, and just connect to the HTTP, it also works. – Ace Jan 09 '18 at 13:39
  • Can you stacktrace the error? – Rex Low Jan 09 '18 at 13:39
  • Additionally, please check if your server cert is correctly configured here https://www.digicert.com/help/ – Rex Low Jan 09 '18 at 13:41
  • 1
    I edited my posted question and included the error. And I checked our url, the digicert said: SSL Certificate is correctly installed. Congratulations! This certificate is correctly installed. – Ace Jan 09 '18 at 13:46

1 Answers1

0

From the look of this, this is most likely an issue of react-native. You may want to test around with different react-native version against different android version. Here's some thread from other users who's having the same problem, most of them resorted with upgrading react-native version. Remember, technically react-native is not a fully grown product yet, so some errors are to be expected.

React Native fetch() Network Request Failed

React-native fetch() from https server with self-signed certificate

React-Native fetch, Network request failed. not using localhost

Rex Low
  • 2,069
  • 2
  • 18
  • 47
  • 1
    Just an update. To solve this problem, I have to upgrade the react-native version at least 0.46 – Ace Jan 03 '19 at 07:59