Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fetch is not working for https in android platform #12903

Closed
EvanGeminika opened this issue Mar 13, 2017 · 38 comments
Closed

fetch is not working for https in android platform #12903

EvanGeminika opened this issue Mar 13, 2017 · 38 comments
Labels
Bug Platform: Android Android applications. Ran Commands One of our bots successfully processed a command. Resolution: Locked This issue was locked by the bot.

Comments

@EvanGeminika
Copy link

Description

I've create a self-signed http & self-sign certificate https server in one app by using express.js
Fetch is working fine when I send the request to http port.
But it is fail when I send the same request to https port.
I understand it might be due to self-sign certificate, but is there any way to ignore error due to self-sign certificate in android ?

Reproduction

fetch('https://192.168.1.21/Register/', { //not working
fetch('http://192.168.1.21:3000/Register/', { //working fine
//the rest of the code is the same.

Solution

Additional Information

  • React Native version: 0.42.0
  • Platform: Android
  • Operating System: Linux (Ubuntu)
  • Dev tools: Android SDK 23
@jqn
Copy link

jqn commented Mar 14, 2017

I'm facing the same problem. Is there a solution to this?

@saniales
Copy link

Also I'm facing this problem

@rnowm
Copy link

rnowm commented Jun 23, 2017

same here

@dave-irvine
Copy link

I have the same issue on iOS. I can't allow self-signed certificates for some reason.

@CaptainTeemo
Copy link

@dave-irvine For iOS, just make an extension for RCTHTTPRequestHandler and implement delegate method - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler.

This will not break the source code of React Native, also verify you certificates as you want.

@Germinate
Copy link

facing the same problem, need help!

@stale
Copy link

stale bot commented Nov 6, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Nov 6, 2017
@safeimuslim
Copy link

Also I'm facing this problem

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Nov 7, 2017
@AAGSICON
Copy link

AAGSICON commented Nov 7, 2017

Also I'm facing this problem. please help me to solve this issue

@tunatoksoz
Copy link

Before [1] this was possible by customizing the okhttp client on android. It's no longer possible.

[1] 0a71f48#diff-f8703d50cf5af4c06824e2928dddb500

@netpedro-com
Copy link

It's a need this fetch API allow self-signed requests. As a workaround, I'm using this package react-native-fetch-blob to make self-signed requests. Please, implement this feature!

@stale
Copy link

stale bot commented Feb 27, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Feb 27, 2018
@react-native-bot react-native-bot added Android Ran Commands One of our bots successfully processed a command. labels Mar 13, 2018
@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Mar 13, 2018
@react-native-bot react-native-bot added Android Ran Commands One of our bots successfully processed a command. labels Mar 18, 2018
@react-native-bot react-native-bot added Platform: Android Android applications. Ran Commands One of our bots successfully processed a command. labels Mar 18, 2018
@woodenlim
Copy link

Still facing this issue in latest RN Version ==

@tiago-marques
Copy link

Same issue here, Generating a debug APK or using react-native run-android I able to fetch the HTTPS URL, but unfortunately with release APK it always timeout (trying to get some logcat logs to help with this issue), I think it as something relates to SSL Exception by Android platform.

react-native info:

Environment:

OS: Windows 10
Node: 9.5.0
Yarn: 1.6.0
npm: 6.0.0
Watchman: 4.9.1
Xcode: N/A
Android Studio: Not Found

Packages: (wanted => installed)

react: 16.3.1 => 16.3.1
react-native: ^0.55.4 => 0.55.4

@tiago-marques
Copy link

tiago-marques commented May 18, 2018

Hi, I have this problem only in one specify WI-FI connection, but in the same network with my computer I am able to access the HTPPS URl and having GET,POST, all working via browser/postman.

I don't know what can be.

@legion-zver
Copy link

Did you solve it?

@tiago-marques
Copy link

In my case, it was a backend problem with certificates that cause Android not getting any responses.

@MadGeorge
Copy link

MadGeorge commented Jul 25, 2018

I have same problem with android sdk < 21
My API endpoints is under cloudflare and all requests failed with network request failed error message when i send any request to https://myCloudflireDomain

Same request works as expected on iOS and Android sdk > 21.

This should be considered as bug in this fetch in reason of my API endpoint can be opened in default android browser on device with android 4.4.2 for example.

Here is what i can observe so far:
Android 4.4.2:
fetch('https://myCloudflireDomain') network request failed
fetch('https://wikipedia.org/') this is works

But! Both domains will work in default browser.

Why it is possible to open https://myCloudflireDomain in android browser, but fetch rase an exception?

PS: SSL cert is definitely valid screen shot 2018-07-25 at 11 48 48
Environment
  • React Native version: 0.56.0
  • Platform: Android
  • Operating System: MacOS

@haily2706
Copy link

haily2706 commented Aug 8, 2018

https://github.com/MadGeorge Are you able to solve this issue. I face the same problem as you. Does anyone have solution for this problem. Please help me, thank you!

@EvanGeminika
Copy link
Author

EvanGeminika commented Aug 8, 2018

A work around that I use for this issue, is to use react-native-fetch-blob.
Because there's an option in RNFetchBlob to ignore security.certification.
We have to set trusty in the config part.
Below is the complete sample:

RNFetchBlob.config({
trusty: true,
timeout: 5000
})
.fetch(
"POST",
"https://192.168.2.10/testService",
{
Accept: "text/xml",
"Content-Type": "application/json"
},
JSON.stringify({
key: keyValue
})
)
.catch(err => {
console.log("err: ", err);
alert("Network Error");
});

But above code will also give this error:
java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

The 2nd work around as per link:
joltup/rn-fetch-blob@48f18c8

is to edit: /node_module/react-native-fetch-blob/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java
and comment: client.sslSocketFactory(new TLSSocketFactory());

@MadGeorge
Copy link

@haily2706 no I’m not. I turned off Cloudflare option to force domain to https this is only solution I found.

@vvavdiya
Copy link

I am facing some problem. My https API was working. but after implementing a self signed certificate for https at the server, https APIs are failing withNetwork request failed.

@legion-zver
Copy link

After switching to 0.57 + the problem is gone

@HEMANT8712
Copy link

I am facing the same issue. my https server working fine with postman using a self-signed certificate. But unable to get any response from the server when using the react-native android app.
Any Suggestions?

@manvi-ivnam
Copy link

manvi-ivnam commented Dec 10, 2018

I added the following code to the onCreate method of MainActivity and everything worked as it should.

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.security.ProviderInstaller;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            ProviderInstaller.installIfNeeded(getApplicationContext());
        } catch (GooglePlayServicesRepairableException e) {
            e.printStackTrace();
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
    }

UPDATE
Sometimes I get broken JSON in the response anyway, so I added the rn-fetch-blob library with isTrusted: true option to the solution as mentioned above. The problems disappeared completely.

@HEMANT8712
Copy link

Dobrynia, Thanks for the suggestion, while I am trying this code, I am getting compilation errors, do I have to install any package for google libraries in react native, please suggest

Task :app:compileDebugJavaWithJavac FAILED
C:\Users\Hemu\Downloads\Prototype-master\Prototype-master\newClient\android\app\src\main\java\com\newclient\MainActivity.java:9: error: package com.google.android.gms.common does not exist
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
^
C:\Users\Hemu\Downloads\Prototype-master\Prototype-master\newClient\android\app\src\main\java\com\newclient\MainActivity.java:10: error: package com.google.android.gms.common does not exist
import com.google.android.gms.common.GooglePlayServicesRepairableException;
^
C:\Users\Hemu\Downloads\Prototype-master\Prototype-master\newClient\android\app\src\main\java\com\newclient\MainActivity.java:11: error: package com.google.android.gms.security does not exist
import com.google.android.gms.security.ProviderInstaller;
^
C:\Users\Hemu\Downloads\Prototype-master\Prototype-master\newClient\android\app\src\main\java\com\newclient\MainActivity.java:25: error: cannot find symbol
protected void onCreate(@nullable Bundle savedInstanceState) {
^
symbol: class Nullable
location: class MainActivity
C:\Users\Hemu\Downloads\Prototype-master\Prototype-master\newClient\android\app\src\main\java\com\newclient\MainActivity.java:29: error: cannot find symbol
ProviderInstaller.installIfNeeded(getApplicationContext());
^
symbol: variable ProviderInstaller
location: class MainActivity
C:\Users\Hemu\Downloads\Prototype-master\Prototype-master\newClient\android\app\src\main\java\com\newclient\MainActivity.java:30: error: cannot find symbol
} catch (GooglePlayServicesRepairableException e) {
^
symbol: class GooglePlayServicesRepairableException
location: class MainActivity
C:\Users\Hemu\Downloads\Prototype-master\Prototype-master\newClient\android\app\src\main\java\com\newclient\MainActivity.java:32: error: cannot find symbol
} catch (GooglePlayServicesNotAvailableException e) {
^
symbol: class GooglePlayServicesNotAvailableException
location: class MainActivity
7 errors

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':app:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 3s
28 actionable tasks: 1 executed, 27 up-to-date
Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/getting-started.html

C:\Users\Hemu\Downloads\Prototype-master\Prototype-master\newClient>

@lahed
Copy link

lahed commented Dec 12, 2018

same error, I use Cloudflare, my version of RN is 0.57.7 😢 @dobrynia don't compile , any fix?

@manvi-ivnam
Copy link

manvi-ivnam commented Dec 12, 2018

@HEMANT8712 and @lahed, is there the
implementation 'com.google.android.gms:play-services-base:16.0.1' line in your android/app/build.gradle file, section dependencies?

@lahed
Copy link

lahed commented Dec 12, 2018

@dobrynia don't work for me.

My solution: I configure Cloudflare to use http and https on my api, and everything else in https.

When android version is < 20 I used http else https

@Titozzz
Copy link
Collaborator

Titozzz commented Mar 19, 2019

Hello there 👋 this issue has been reported for an old version of React Native. Ideally we'd like everyone to be using 0.59 (see the awesome changes it brought) but we know updating can be a pain. During 0.59 update, some changes were made to the android JSC, so it might have fixed this.

But please, if it's actually still an issue with 0.59 please comment below and we can reopen it. Even better, please send us a pull request with a fix 😊

@Titozzz Titozzz closed this as completed Mar 19, 2019
@andriadze
Copy link

andriadze commented Apr 18, 2019

Still an issue on react 0.59 for me.

My configuration is almost same as @MadGeorge
I have Cloudflare configured to force my domain to https. This works fine on Androids above 5.0 and iOS too, but fails on older Android devices.

Switching to http helps

@mi-mazouz
Copy link

I'm facing the same issue

@gomdolkim
Copy link

I am facing same issue

1 similar comment
@alikianinejad
Copy link

I am facing same issue

@jemmyphan
Copy link

try this
it works for me

@sagarrs
Copy link

sagarrs commented Jan 14, 2020

same issue RN version 0.61.4

@d1sd41n
Copy link

d1sd41n commented Jan 17, 2020

2020 and the same problem with axios :(

@mi-mazouz
Copy link

I solved my problem with axios by passing data anyway.

{ data: data || {} }

@facebook facebook locked as resolved and limited conversation to collaborators Mar 20, 2020
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Mar 20, 2020
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Bug Platform: Android Android applications. Ran Commands One of our bots successfully processed a command. Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests