ionic4ionic-nativecordova-plugin-advanced-http

Ionic-native HTTP POST method not working on iOS (but perfectly working on Android)


I'm using @ionic-native/http in my Ionic 4 project for logging in user by sending body with UserId and Password and header with 'Content-Type': 'application/json' through POST method. Its working fine in android and but on iOS it responding with a http 400 error.

Dependencies: 

"@ionic-native/http": "^5.3.0",

"cordova-plugin-advanced-http": "^2.0.9",

I tired using the @angular/http but its giving a CORS error on browser, android and iOS. And I can't change the server side code to enable CORS.

Code:

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

// Login method  
async login(userId, password) {
    // login user
    const httpBody = {
      'UserId': userId,
      'Password': btoa(password)
    };
    const httpHeader = {
      'Content-Type':  'application/json',
    };
    const user = await this.http.post(this.loginURL, httpBody, httpHeader);
    return JSON.parse(user.data);
  }

Expected result response with StatusCode 200

Actual Result response with StatusCode 400


Solution

  • I waas having the same issue; in my case I fixed it by setting the data serializer to 'utf8' and by setting the Content-Type header to 'application/x-www-form-urlencoded'

    this.http.setDataSerializer('utf8');
    const httpHeader = {
      'Content-Type':  'application/application/x-www-form-urlencoded'
    };