React Native Fetch Request Fails
Asked Answered
H

5

11

I am using the react native Fetch API to get JSON data from https://api.github.com/users/{username} but the request fails with the following error message.

"TypeError: Network request failed {stack: (...), message: 'Network request failed'}".

I believe for https, sometimes you get NSURLAuthenticationChallenge. I am not sure how to implement this. Anyone have any idea about this?

Hypostasis answered 27/4, 2015 at 16:34 Comment(2)
it's difficult to tell without the code snippet, but you could try adding / to the end of the url you're using. also interpolation is done using #{username}Pickering
Can you provide some minimal code to reproduce this error? One way to get it is to try to fetch an invalid URL, but there's no way to say what caused it in your case without you showing some code.Jenaejenda
N
2

If you are using iOs you may need to do the following:

You must enable your AppTransportSecurity

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

or info.plist

in the Info.plist.

For Android ensure that you have added permission to use INTERNET in the AndroidManifest.xml.

<uses-permission android:name="android.permission.INTERNET" /> 
Nablus answered 26/3, 2018 at 1:29 Comment(0)
F
1

Can you provide an snippet of your fetch code?

Generally, a fetch statement is written like:

fetch(requestURL)
  .then( (response) => response.json() )
  .then( (data) => {
    this.setState({data: data});
   })
  .catch( (error) => console.log(error) )
  .done();

Catching the error will allow the program to proceed without crashing.

Farnsworth answered 12/5, 2015 at 4:53 Comment(0)
B
0

In my case the issue wasn't in the code. I started emulator when had no network. After I logged in to wifi point network on emulator still wasn't functioning. I restarted emulator - all worked.

So:

  1. Connect to network.

  2. Restart emulator.

If it doesn't help, then check your code.

Burbank answered 26/3, 2016 at 17:16 Comment(0)
B
0

I was also having this issue React Native Fetch Request Fails very frequently.

In my case the response from the API call was around 5kb , so i removed the unnecessary data from the API response and reduced the result size to around 1kb and all started working.

So try to limit the data you are requesting from the API.

Beneath answered 25/5, 2016 at 7:30 Comment(0)
R
-3

Have you tried XMLHttpRequest?

As demonstrated in the doc, https is supported by XMLHttpRequest:

var request = new XMLHttpRequest();
request.onreadystatechange = (e) => {
  if (request.readyState !== 4) {
    return;
  }

  if (request.status === 200) {
    console.log('success', request.responseText);
  } else {
    console.warn('error');
  }
};

request.open('GET', 'https://mywebsite.com/endpoint.php');
request.send();
Radke answered 2/5, 2015 at 14:7 Comment(1)
In the same doc that you posted, it shows that you can do https with Fetch as wellStockbroker

© 2022 - 2024 — McMap. All rights reserved.