7

I have a VM running locally that's built using Vagrant. I am able to curl and go to the URL directly from the browser. For some reason, when I make the same call in my react-native app using the fetch API, it keeps giving me the Network request failed error.

Here is a snippet of the code:

fetchData() {

  this.setState({ isLoading: true });

  var baseURL = 'https://192.168.33.33/api/session';

  console.log('URL: >>> ' + baseURL);

  fetch(baseURL)
  .then((response) => response.json())
  .then((responseData) => {
    console.log(responseData);
  })
  .catch(error => {
    console.log(error);
  })
  .done();
}

The baseURL log there returns the right URL and the error looks like so:

URL: >>> http://192.168.33.33/api/session
TypeError: Network request failed {stack: (...), message: "Network request failed"}
  message: "Network request failed"
  stack: (...)
  get stack: function () { [native code] }
  set stack: function () { [native code] }
  __proto__: Error

I thought it might've been an issue with my VM not being accessible somehow by my app, so I went ahead and deployed it to a real server and it still gave the same error.

Any ideas?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Koes Bong
  • 1,113
  • 3
  • 15
  • 28

2 Answers2

4

Found the problem and solved it.

The issue was caused by having self-signed certificate in the API server without a self created CA.

I could try creating my own CA and then create a certificate from that but I went ahead and got a cheap real SSL cert. That solved the issue.

Koes Bong
  • 1,113
  • 3
  • 15
  • 28
-1

I think you might be facing the same origin policy constraint.

There are many ways of workaround it.

Community
  • 1
  • 1
Christian
  • 7,062
  • 9
  • 53
  • 79