Hey, I have been upgrading my Ionic platform and I have been able to get all of the components working on the new Ionic 5 platform but there is only this one API that is not working. I have tested on the android application and it was working fine to make this API call. But on the IOS side it does not work. I did receive a CORS error earlier with other API responses but I was able to resolve those issues so Im not sure why this API call is failing. Also I have tested on Postman and the server is working fine.
Any help is appreciated.
import {throwError as observableThrowError, Observable} from 'rxjs';
import {catchError, map} from 'rxjs/operators';
import {Component, ViewChild} from '@angular/core';
import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders, HttpRequest} from '@angular/common/http';
import {env} from '../env/env';
import {ToastController, IonApp, NavController} from '@ionic/angular';
import 'rxjs/add/operator/toPromise';
import {NavigationExtras} from '@angular/router';
import { Storage } from '@ionic/storage';
@Injectable()
export class ApiService {
private userID: string;
private userNotification: string;
private userDetails: string;
private userToken: string;
constructor(
public storage: Storage,
private http: HttpClient,
public toastCtrl: ToastController,
public appCtrl: NavController,
// public device: Device
) {
}
inviteFriend(mobile, username){
return this.http.get(`${env.p3ApiUrl}/users/invite/${mobile}/${username}`).pipe(
catchError(this.handleError)).toPromise();
}
usage:
inviteFriend() {
if (!this.contact.name) {
this.ApiService.showToast('Please enter name');
return false;
}
if (!this.contact.mobile) {
this.ApiService.showToast('Please enter mobile');
return false;
}
if (this.contact.mobile.length != 10) {
this.ApiService.showToast('Please enter a valid mobile number');
return false;
}
for (let i = 0; i < this.friends.length; i++) {
if (this.friends[i].mobile == this.contact.mobile) {
this.ApiService.showToast('You\'ve already invited a contact with ' + this.contact.mobile);
return false;
}
}
this.loadingCtrl.create({
message: 'Inviting friend ...'
}).then((res) => {
res.present();
let userDisplayName;
this.ApiService.getUserDetails()
.then((r: any) => {
userDisplayName = r.firstName + ' ' + r.lastName;
this.ApiService.inviteFriend(this.contact.mobile, userDisplayName)
.then((r: any) => {
console.log(r);
if (r.status == 200) {
this.showUserExistsAlert(this.contact);
}
if (r.status == 201) {
this.addFriend(this.contact);
}
res.dismiss();
})
.catch(e => {
console.log(e);
res.dismiss();
if (e.status == 400) {
this.ApiService.showToast('Please enter a valid mobile number');
}
});
})
.catch(e => {
res.dismiss();
});
});
}
error:
[Log] HttpErrorResponse (cordova.js, line 1540)
error: Event {isTrusted: false, total: 0, totalSize: 0, loaded: 0, position: 0, …}
headers: HttpHeaders
headers: Map {} (0)
lazyUpdate: null
normalizedNames: Map {} (0)
HttpHeaders Prototype
message: "Http failure response for https://link-URL: 400 Bad Request"
No Properties
name: "HttpErrorResponse"
No Properties
ok: false
No Properties
status: 400
No Properties
statusText: "Bad Request"
No Properties
No Properties
HttpErrorResponse Prototype
constructor: function(init)
HttpResponseBase Prototype