Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Fetch Request Fails

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?

like image 324
sanjeev Avatar asked Apr 27 '15 16:04

sanjeev


People also ask

Does fetch work with react Native?

Using Fetch​React Native provides the Fetch API for your networking needs. Fetch will seem familiar if you have used XMLHttpRequest or other networking APIs before. You may refer to MDN's guide on Using Fetch for additional information.

How do you catch network request failed in react native?

It's just that simple! Start your app as usual but don't forget to give an IP address and a port, this will help you solve the Network Request Failed error. And on your mobile app, make sure to use the correct URL in your request. Make sure that CORS is enabled on your backend to avoid errors related to CORS.

What does it mean when it says network request failed?

Most of time , when network request failed error occurred, clearing the app cache will resolve the issue. So, To fix Instagram network request failed issue, clear Instagram app cache. To clear the Instagram App cache, For Android users, Go to settings >> Find Instagram App >> Tap on clear cache.

What is network request?

A network request is an HTTP request from your mobile app to a server-side application. The iOS Agent detects network requests when the underlying implementation is handled by the NSURLConnection or NSURLSession classes.


2 Answers

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" /> 
like image 153
Jojo Narte Avatar answered Oct 09 '22 19:10

Jojo Narte


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.

like image 30
Jonathan Huang Avatar answered Oct 09 '22 19:10

Jonathan Huang