Native HTTP plugin error

Hi there,
I have been trying to get an API to work along with my ionic (V4) app with no luck.
I have tried using Http and HttpClient provided by angular but it raised some errors and after a quick look on the internet, it seemed like this was an error related to CORS. Before you ask, I didn’t run the app through ionic serve -c but I ran ionic cordova build ios and ran the app on my iPhone from Xcode.
Now I’m trying to use the Native HTTP plugin but I get an error that I can’t find a solution for. I will be glad to get some answers (app due to this Sunday :scream:

My error (as shown in Safari Web Inspector):

{"line":67,"column":24,"sourceURL":"ionic://localhost/plugins/cordova-plugin-advanced-http/www/helpers.js"}

When I click the js file link I get this function:

function checkKeyValuePairObject(obj, allowedChildren, onInvalidValueMessage) {
    if (getTypeOf(obj) !== 'Object') {
      throw new Error(onInvalidValueMessage);
    }

    var keys = Object.keys(obj);

    for (var i = 0; i < keys.length; i++) {
      if (allowedChildren.indexOf(getTypeOf(obj[keys[i]])) === -1) {
        throw new Error(onInvalidValueMessage); <== line 67
      }
    }

    return obj;
  }

Thank you for answers.

1 Like

Did you fix this? I am currently stuck here as well… due last week!

I have the exact same error. Did you guys find a solution?

I found the error! I was setting the headers to
{ headers: { 'Content-Type':'application/json'} }
instead of
{ 'Content-Type':'application/json'}

Hope it helps.

1 Like

For Any one who is still searching for solution…

My Old code was (using HttpClient by angular)

let contentHeaders = new Headers();
contentHeaders.set('Accept', 'application/json');
contentHeaders.set('Content-Type', 'application/json');
let qp = new URLSearchParams();
qp.set('action', 'countries');
this.http.get(API_DOMAINNAME+'api1/mssql.php',{ search:qp, headers: contentHeaders})

My New Code (using HTTP from @ionic-native/http)

let contentHeaders ={
'Accept': 'application/json',
'Content-Type':'application/json'
};
let qp = {action:'countries'};
this.http.get(API_DOMAINNAME+'api1/mssql.php', qp,contentHeaders)

I hope people do not conclude from the post above this one that they should be using @ionic-native/http instead of Angular HttpClient, because in the vast majority of cases, the opposite is true, because HttpClient is so much friendlier to use. Just emulate the official documentation.