Ionic native Http post not working in IOS

1.5k Views Asked by At

I need help in resolving this issue. I’m facing issue when doing POST request to get the access token in IOS. Whereas it works fine in Android.

I'm using the Cordova plugin cordova-plugin-advanced-http: "^2.4.1".

Code:

const bodyParams = {‘client_id’:clientId,‘client_secret’:secret,‘grant_type’:‘authorization_code’,‘code’:tokenOrCode};
const httpResponse = await this.http.post(accessTokenUrl, bodyParams, {“Content-Type”: “application/json”});

Error:

{“error”:“invalid_client”,“error_description”:“FBTOAU204E An invalid secret was provided for the client with identifier: ‘[email protected]’.”}

Note: The above code works without any issues in Android.

Package.json:

"@angular/common": "^7.2.2",
"@angular/core": "^7.2.2",
"@angular/forms": "^7.2.2",
"@angular/http": "^7.2.2",
"@angular/platform-browser": "^7.2.2",
"@angular/platform-browser-dynamic": "^7.2.2",
"@angular/router": "^7.2.2",
"@ionic-native/core": "^5.0.0",
"@ionic-native/http": "^5.2.0",
"@ionic-native/network": "^5.24.0",
"@ionic-native/secure-storage": "^5.2.0",
"@ionic-native/splash-screen": "^5.0.0",
"@ionic-native/status-bar": "^5.0.0",
"@ionic/angular": "^4.1.0",
"ajv": "^6.10.0",
"cordova-ios": "^4.5.5",
"cordova-plugin-advanced-http": "^2.0.9",
"cordova-plugin-cookiemaster": "^1.0.5",
"cordova-plugin-device": "^2.0.3",
"cordova-plugin-file": "^6.0.2",
"cordova-plugin-ionic-keyboard": "^2.2.0",
"cordova-plugin-ionic-webview": "^3.0.0",
"cordova-plugin-network-information": "^2.0.2",
"cordova-plugin-secure-storage": "^3.0.2",
"cordova-plugin-splashscreen": "^5.0.3",
"cordova-plugin-statusbar": "^2.4.3",
"cordova-plugin-whitelist": "^1.3.4",
"core-js": "^2.5.4",
"rxjs": "~6.3.3",
"zone.js": "~0.8.29"
2

There are 2 best solutions below

0
On

I was having a problem similar to your problem like request was working on Android, was not working on Ios. (Ionic 5 - Capacitor App)

I fixed my problem to changing my content-type.

It may help you too, please try it.

{“Content-Type”: “application/json”}

Replace with the following

{“Content-Type”: “application/x-www-form-urlencoded”}
5
On

Before sending post request set serializer as per your request :

For json :

this.http.setDataSerializer("json");
const bodyParams = {‘client_id’:clientId,‘client_secret’:secret,‘grant_type’:‘authorization_code’,‘code’:tokenOrCode};
const httpResponse = await this.http.post(accessTokenUrl, bodyParams, {“Content-Type”: “application/json”});