HTTP 400 error Bad Gate way only on IOS

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

You may want to edit your topic title, because “bad gateway” is 502 and a completely different animal from a 400 “bad request”. 400s typically (well, at least when I write backends) are the backend telling you that you fed it something it didn’t want to eat, such as leaving out a required parameter or giving an unsupported value for one.

I would suggest looking in the server logs as a potential source of additional useful information in diagnosing the problem.

Hi, I checked the server logs and there doesnt seem to be any errors logged. Does that mean the ios app is not making the request through the application?

Every case I’ve seen where the problem lies completely within the device results in a status code of 0, so it would surprise me if there wasn’t at least some interaction with the server. Incidentally, now that I reread what this call at least seemingly is doing, I’m very concerned that it should not be a GET response at all, because it is performing an action. GET requests need to be read-only and cacheable. If your backend ever ends up being hosted behind a caching proxy, these requests may never see your server at all. In fact, that could even be an explanation for why you’re not seeing anything in server logs right now. I would try changing this operation to be a POST instead.

Thank you for the suggestion, but if I’m able to receive responses on my android devices and this is only happening on the IOS device is there a possible workaround on the application side instead of changing the server request?

Hey, I was able to convert the GET request to a POST request. did all the testing on the Android and was working fine again, but for some reason on the IOS I am still getting the same error. this is the updated API request made:

inviteFriend(mobile, username){
      return this.http.post(`${env.p3ApiUrl}/users/invite/${mobile}/${username}`, {}, {}).pipe(
              catchError(this.handleError)).toPromise();
  }

this is the error:

[Log] HttpErrorResponse (cordova.js, line 1540)

error: Event {isTrusted: false, total: 0, totalSize: 0, loaded: 0, position: 0, …}

headers: HttpHeaders {normalizedNames: Map, lazyUpdate: null, headers: Map, has: function, get: function, …}

message: "Http failure response for: 400 Bad Request"

name: "HttpErrorResponse"

ok: false

status: 400

statusText: "Bad Request"


HttpErrorResponse Prototype

You said before that Postman was working fine. Can you use Safari’s remote debugging feature (or something like wirershark) to capture the exact request that is going across the network from the iOS device and then compare it to the request you are making with Postman? One would think there would have to be some difference that could shine light on the issue.

Hey so I took a recording of the request that is being made from the IOS emulator. I’m not sure why it is still sending a GET request to the server when I have changed it to a POST request.

Is it possible that you’re running an older version of the app there than you think?

Nope, I reinstalled a couple times to make sure and I tested on android with the server change again to make sure and it is working fine on android.

Is there any other information that may be helpful in resolving this issue because it is still persisting?