Ionic-native/http/ngx v.s. @angular/common/http which should be used in an Ionic4 app?

Hi,
I can see that you can use two libraries to get/post data.

The first is @ionic-native/http/ngx:

import { HTTP } from '@ionic-native/http/ngx';
constructor(private http: HTTP) {}
return this.http
	.post(...)
	.then(..)
	
/* Returns a Promise */

the second is @angular/common/http:

import { HttpClient } from '@angular/common/http';
constructor(private httpClient: HttpClient)
return this.httpClient
	.post(...)
	.subscribe(..)
	
/* Returns an Observer */

I have to use a REST API with Https.
Which is the right solution, according to your experience?
Why?

Thank you very much

If there was an objectively “right solution” here, then one of these options wouldn’t exist. In the vast majority of situations, though, the Angular HttpClient will be easier to use, as it simplifies a lot of boilerplate code. I would suggest using HttpClient as your default, and only looking at the Ionic-Native HTTP when you run into a specific problem that it addresses (such as manual SSL certificate pinning).

2 Likes

Thank you.
At the moment I’m using HttpClient but I have had problems since I modified the REST API from Http to Https.
When “manual SSL certificate pinning” is needed?
Perhaps this why my app isn’t working with Https.
I’ve done both the app and the rest API on the server and I’m using a Let’s Encrypt certificate on the server.
What is the case in which I can’t use HttpClient?

Thank you very much

cld

All I can tell you is that I have written many apps with the exact structure you describe (Let’s Encrypt cert for server used by Ionic app) and had no trouble using the Angular HttpClient for sending HTTPS requests to it.

You might have a CORS issue. Here is some comprehensive documentation on CORS; you may find more searching this site, but I would suggest ignoring anything you come across that mentions adding anything to headers on the Ionic side - CORS needs to be dealt with on the server side.

1 Like

If it had been a CORS issue, shouldn’t I have had it with http too?
The app works with http.
But I know, this should be another thread.

Excuse me @rapropos did you use always the port 443 for https?
Because I’m using the port 4433.

Because I begin to think that the problem could be one of this.

  • HTTPS needs the port 443
    or
  • My certificate is valid only for the 443 port and I have to create another one for the port 4433.

Have you experienced this issue?

Thank you very much

cld

You can serve https over any port and certificates are not tied to port numbers (if you have a copy of the cert open and read the data). Is 4433 just on your dev machine? Certs are tied to your domain so if the cert says mysite.com or www.mysite.com and you are serving on localhost that won’t work.

Ok guys,
I have solved.

The problem was on the server.
The server uses node.js, when I’ve replaced these credentials:

cert: fs.readFileSync('./sslcert/chain.pem'),
key: fs.readFileSync('./sslcert/privkey.pem')

with these:

    cert: fs.readFileSync('./sslcert/fullchain.pem'),
    key: fs.readFileSync('./sslcert/privkey.pem')

So the fullchain is required.

Now httpClient seems to work both on Android and iOS.

cld