TypeError: Network request failed, when fetching API request

4.7k Views Asked by At

I am encountering an error when fetching a locally deployed API(Firebase). Pasting the API link in the web browser works, but not in my react-native app. When calling the function, it returns a Network Request Failed Error, when I check in the Debug, there is no specific Error. Here is the fetch function:

const SignInPress = async () => {
    try {
      const response = await fetch(
        'http://127.0.0.1/:5001/freelance-services-api-a0277/us-central1/app/api/freelancers',
        {
          method: 'GET',
          headers: {
            'Content-Type': 'application/json',
            'ACCESS-CONTROL-ALLOW-ORIGIN': '*',
          },
        },
      );
      const json = await response.json();
      return json;
    } catch (error) {
      console.error(error);
    }};

I have tried different methods such as changing the IP address and modifying the Android.xml file so that it accepts http links, but the error still exists.

1

There are 1 best solutions below

0
Boopy On

To do a request to your local machine, you must to use your local IP, after you need to update your Android and iOS configurations.

You can see this link: Connecting React Native to localhost

There is also an issue like that about fetch local data on Android: React Native Android Fetch failing on connection to local API