Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to make API calls using react-native

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?

like image 689
Koes Bong Avatar asked May 26 '15 05:05

Koes Bong


1 Answers

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.

like image 165
Koes Bong Avatar answered Nov 09 '22 06:11

Koes Bong