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?

5
  • does https://192.168.33.33/api/session maybe require some kind of headers on your get request? Commented May 26, 2015 at 6:29
  • @lyjackal I don't think so, all the code does is this: d.pr/n/18BiU/3kXPWHcD Commented May 26, 2015 at 14:54
  • If you use the XMLHttpRequest object instead of fetch, do you get the same error? Is CORS setup correctly? Commented Jun 1, 2015 at 7:02
  • Can you provide us with the URL of your real server so that we can possibly reproduce the error? Commented Jun 2, 2015 at 8:27
  • Did you check the same origin policy? Commented Jun 2, 2015 at 21:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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

There are many ways of workaround it.

3 Comments

Thanks for your answer! unfortunately, that wasn't the case.
This doesn't apply to react native apps.
same origin policy is only for the browser!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.