Ionic 4: How do I trust Self-Signed Cert using ionic-native/http/ngx?

2.3k views Asked by At

I am trying to use HTTP ionic-native/http/ngx in place of HTTPClient angular/common/http.

I tried to follow examples from : https://ionicframework.com/docs/native/http

https://www.freakyjolly.com/ionic-native-http-plugin-tutorial-with-example/#.XyHQ8igzaUk

https://www.youtube.com/watch?v=ApSskiMT2_8

but encountered many errors such as "Error cordova not available".

Not sure where I went wrong, but these are the steps I followed:

(1)Install Native HTTP Plugin

(2)Import in App.component.ts and under initialiseApp() (this.http.setServerTrustMode("nocheck")....)

(3)Import in App Module

(4)Inject HTTP into the constructor of home.page.ts (by the way, my actual function makes the HTTP call resides in a service)

(5)Invokes the HTTP call using http.sendRequest

What am I missing out? My objective is very simple: I want to be able to let Ionic bypass the self-signed certificate check OR to trust the self signed cert in Ionic.

1

There are 1 answers

0
Nischaya Sharma On

Make sure you are running the app on a Real Device. For logs you can connect to your android studio and open the logcat from the bottom panel. And in app.module.ts make sure you are providing HTTP module rather than importing. I personally have made it a habit to provide all the native plugins I install. So your @NgModule of app.module.ts will look like this:

import { HTTP } from '@ionic-native/http/ngx';

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [BrowserModule,HttpClientModule, IonicModule.forRoot(), AppRoutingModule],
  providers: [{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }, HTTP],
  bootstrap: [AppComponent],
})

And thanks for answering my dowbu of how to ignore the ssl certificate check. this.http.setServerTrustMode('nocheck') worked like a charm for me.