3

I am attempting to get my React Native application working on a real iPhone so I can begin integrating the app with branch.io. For some reason I cannot get my app to make network requests to my backend api on AWS. My code works as expected in the simulator when I hit the backend api on my local dev environment with localhost:3000/auth/login/. Below are the results of my efforts.

HTTP Class Post Method:

post(endpoint, body, auth = null) {

  console.log(this.apiUrl + endpoint);
  console.log(this._headers(true));
  console.log(body);

  return Observable.defer(() => {
    return Observable.fromPromise(fetch(this.apiUrl + endpoint, {
      method: 'post',
      headers: this._headers(auth),
      body: JSON.stringify(body)
    })
    .then(this._checkStatus)
    .then(res => res.json()));
  });
}

_headers(auth) {
  let token = (auth) ? token = Config.clientId : this.accessToken;
  let headers = new Headers({
    'Content-Type': 'application/json',
    'Authorization': 'Bearer ' + token
  });

  return headers;
}

After building the code to my iOS device I attempt to login and nothing happens. I added console calls to my post request to make sure the proper values are being passed and that seems to be correct. Below is the results of what I see when I attempt to login while running debug mode. I am new to React Native so I am not sure if the network tab would detect requests coming from a real device, nonetheless there is no sign of any network call being made.

enter image description here

Running the same exact call from Postman works as expected and can be seen below:

enter image description here

Any assistance would be greatly appreciated. Thanks you in advance.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Aaron
  • 2,364
  • 2
  • 31
  • 56
  • I think it’s vecause you’re trying to access an http url. You’ll need to allow the app to access http in the ATS settings in the app config. Not sure how with your ide but you should be able to google it. – Fogmeister Oct 30 '17 at 12:09
  • Thank you. That worked. I actually tried this before and for some reason it wasn't working. Not sure what I did wrong. For sure it was developer error. I am including the stack page I referenced before: https://stackoverflow.com/questions/31216758/how-can-i-add-nsapptransportsecurity-to-my-info-plist-file – Aaron Oct 30 '17 at 12:23

1 Answers1

0

Because you are likely using HTTP and not HTTPS, you need to enable NSAppTransportSecurity and add your domain to the NSExceptionDomains list in your info.plist.

You can disable the security check entirely by turning on NSAllowsArbitraryLoads. However, while useful for development purposes, it is always better to make your development environment work more like your release or production, thereby minimizing bugs and regressions. (There is a bonafide reason to use NSAllowsArbitraryLoads in production, but if you are just talking to one backend, then you do not need it.)

You can learn about the settings for ATS here (go to ATS Configuration Basics).

greymouser
  • 3,133
  • 19
  • 22