cordova-plugin-advanced-http vs @angular/common/http
Asked Answered
A

1

6

I want to create an API helper class to serve for all types of rest api calls in the app. I have been using the angular httpClient class

import {HttpClient} from '@angular/common/http'

to do API interaction but recently came across https://ionicframework.com/docs/native/http/

Can anybody describe the difference between the two so that I could decide which one would be better as per my requirement.

Alyosha answered 2/1, 2019 at 8:5 Comment(1)
HttpClient will send http requests from webview without using native http calls. It may produce CORS issue if your api server is not configured to serve cross origin requests.Prenatal
R
9

For starters, Angulars Http Module is based on Observables which are widely used within Angular and thus Ionic themselves. The Ionic Http Module instead is based on Promises, which itself is totally fine if you want to use them.

Since this is an Ionic Native Module this also means that the app is not going to make these requests using either the underlying Java (Android) or Objective-C (iOS) function instead of a Javascript function (Please anybody correct me if I am wrong here)

However the main advantages are listed on the Github Repo of the Ionic Http Module:

  • Background threading - all requests are done in a background thread.
  • Handling of HTTP code 401 - read more at Issue CB-2415.
  • SSL Pinning - read more at LumberBlog.

The first point seems to be the most appealing, since it is a feature which is not merely solving a problem but adding functionality. I cannot tell exactly how big of an advantage it is to run these in a background thread but I also doubt that this will have a big impact unless your app is very network-expensive.

So Ionics native module solves exactly these problems which apparently come with using plain Javascript functions for making http requests from a containered mobile app.

I would say that the Ionic Native solution should only be used in case you encounter any of the above problems. If not, you would simply add a new dependency which uses another paradigm (promises) than probably any tutorial on Angular which you can find. You simply reduce complexity and bundle size by not using it if it isn't needed.

Angular comes bundled with the Http Module and it is well maintained, also you will find much more help online for this package.

Represent answered 2/1, 2019 at 9:43 Comment(3)
Greatly explained. By your explanation I feel I should go with angular httpClient. But the reason I got attracted towards ionic native http is, "setHeader(host, header, value)" >> Set a header for all future requests. Takes a hostname, a header and a value. Is something similar available in angular httpClient ?Alyosha
Sadly I believe that this is not provided by the Angular module itself. However it is really easy to achieve the same thing. One way is to go ahead and write your own request-service which wraps the angular http methods and set the headers there. Another way to achieve the same thing would be to write an interceptor, which modifies each request: theinfogrid.com/tech/developers/angular/…Represent
I'll look into interceptor.. Thanks for your time. I'll accept your answer :)Alyosha

© 2022 - 2024 — McMap. All rights reserved.