Ionic native HTTP - Service call error


#1

I have installed Cordova advanced-HTTP. Since I have to enable SSL pinning. Using HTTP client my code was working. Now I am changing my code to native Http.

Service Provider class

//MARK:- HTTP POST method

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

  postService(path: string, isLogin: boolean, serviceBody: any) {
   
    var APIFULLURL =  this.APIBASEURL + path;
    //*****************Service Log ********************//
    console.log("==URL ===" + APIFULLURL);
    //*****************Service Log ********************//
    try {

      // this.http.setDataSerializer("json");
      this.http.setHeader('Content-Type', 'application/json; charset=utf-8')
      this.http.setHeader('Authorization', this.APIHeaderKey)


      this.http.setRequestTimeout(40);

      return this.http.post(APIFULLURL, JSON.stringify(serviceBody), { 
        'Content-Type' : 'application/json; charset=utf-8',
        'Content-Encoding': 'gzip',
        'Accept' : 'application/json',
        'Authorization': this.APIHeaderKey,
        'Access-Control-Allow-Headers': 'Accept, Content-Type, Content-Length, Accept-Encoding, Authorization, Origin'


      });

    } catch (error) {
      console.log(error);
      alert("Error" + error)
    }
  }

and my calling place:

 loginFRCService(value: any, loggedInBy: string): Promise<any> {
    return new Promise<any>(resolve => {
     //TO-DO calling web service
     //login
      var username: string = '';
      var password: string = '';
      var socialid: string = '';
      var fingerprintId: string = '';

    let body = {
    'EmailID': username,
    'LoginType': loggedInBy,
    'Password': password,
    'SocialMediaID': socialid,
    'FingerPrintID': fingerprintId

  };

  this.serviceProvider.postService('Account/Authenticate', true, body).then((data : any) => {

    console.log('Login Done!' + data);
    resolve(data.data);

  }).catch((error : any) => {
      console.log("========error")
      console.log(error);
      console.log(error.status);
      console.log(error.error); // error message as string
      console.log(error.headers);
      resolve("ServiceError");

    });
});

When I run the code, I got the error message.

=======error

{"line":217,"column":20,"sourceURL":"http://localhost:8080/Users/chandraat/Library/Developer/CoreSimulator/Devices/55FCEB42-21D5-43AE-B12E-68831F0E6D13/data/Containers/Bundle/Application/5981BD68-A1FD-44B4-A018-146E1C046349/dadsadsadsad.app/www/plugins/cordova-plugin-advanced-http/www/helpers.js","__zone_symbol__currentTask":{"type":"microTask","state":"notScheduled","source":"Promise.then","zone":"angular","cancelFn":null,"runCount":0}}

Please help me out.

anything wrong in my code? Please any help?


#2

Format your code correctly (again).

There is no reason to wrap async code in try/catch unless you use async/await (again).

Setting the headers and timeout are global values, you only have to do that once so you don’t have to put that here.

What in the world is this, where does it come from, and why is it there:
resolve("ServiceError");

I feel like you don’t understand basic JavaScript concepts and should perhaps take a step back from certificate pinning and learn some basics about asynchronous programming.


#3

resolve("ServiceError"); this method is calling from another async method.

I am native ios developer. This is the first time I am developing ionic. My code was working fine with HTTP client. Now I am changing to native HTTP. I am struggling a lot to sort out.

I commented out SSL pinning part. When I run the app, that time I got that above error.

and this is my calling part

loginFRCService(value: any, loggedInBy: string): Promise<any> {
    return new Promise<any>(resolve => {
     //TO-DO calling web service
     //login
      var username: string = '';
      var password: string = '';
      var socialid: string = '';
      var fingerprintId: string = '';

    let body = {
    'EmailID': username,
    'LoginType': loggedInBy,
    'Password': password,
    'SocialMediaID': socialid,
    'FingerPrintID': fingerprintId

  };

  this.serviceProvider.postService('Account/Authenticate', true, body).then((data : any) => {

    console.log('Login Done!' + data);
    resolve(data.data);

  }).catch((error : any) => {
      console.log("========error")
      console.log(error);
      console.log(error.status);
      console.log(error.error); // error message as string
      console.log(error.headers);
      resolve("ServiceError");

    });
});

}

{"line":217,"column":20,"sourceURL":"http://localhost:8080/Users/chandraat/Library/Developer/CoreSimulator/Devices/55FCEB42-21D5-43AE-B12E-68831F0E6D13/data/Containers/Bundle/Application/6421637B-F3F7-4B27-8E9E-A469177A9A3C/myapp.app/www/plugins/cordova-plugin-advanced-http/www/helpers.js","__zone_symbol__currentTask":{"type":"microTask","state":"notScheduled","source":"Promise.then","zone":"angular","cancelFn":null,"runCount":0}}

Please let me know any issues from my code. Since this code was working in http client


#4

Same problem here. Solutions?