[React Native] EXPO - AxiosError: Network Error on Android but not on IOS

Description

**### Describe the bug **

On Android, with Axios my request is returning “AxiosError: Network Error”.
But it all work fine on IOS and with POSTMAN too.

The api call uses HTTPS and is not local

### Solution tried:
I have tried many headers type but none works.
Also the ““usesCleartextTraffic”: true” in “AndroidManifest.xml” and with “expo-build-properties” is not doing anything.
Downgrading axios at any version do not change anything.
The SSL respond good when I test them with some website SSL tester online.
All the TLS1, TLS2, TLS3 are working.

Can you please explain to me what is wrong?
Thank you in advance.

### Code snippet of the API request:


const BASE_URL = process.env.EXPO_PUBLIC_API_URL;

const AUTH_URL = `${BASE_URL}auth/`;
const API_URL = `${BASE_URL}api/`;

export async function auth(
  username: string,
  password: string,
  withStoreUpdate: boolean = false
): Promise<{ isAuth: boolean; currentUser?: object }> {
  try {
    const data = { username, password };

    console.log("Auth is Starting");
    const response = await axios({
      method: "POST",
      url: `${AUTH_URL}login`,
      headers: {
        // Set the Content-Type to application/json
        "content-type": "multipart/form-data",
        // Set the Authorization header to JWT followed by the actual token
        authorization: "JWT",
        // Set the Accept header to application/json
        accept: "application/json",
        // You may not need this unless you are making cross-origin requests
        "ccess-Control-Allow-Origin": "*",
      },
      data,
    })
      .then((response: { data: apiData }) => {
        return response.data;
      })
      .catch((error: any) => {
        throw new Error(error);
      });

    myInfos = response;

    if (response) {
      const newAmbassador = new Ambassador(response.user);

      if (withStoreUpdate)
        store.dispatch(
          updateUserWithLocalStorage({
            ...newAmbassador.json,
            password: password,
            accessToken: response.accessToken,
          })
        );
      else
        store.dispatch(
          updateUserInfos({
            ...newAmbassador.json,
            accessToken: response.accessToken,
          })
        );

      return { isAuth: true, currentUser: newAmbassador };
    } else return { isAuth: false };
  } catch (error) {
    if (error.response) {
      console.log("icierror");
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx
      console.log(error.response.data);
      console.log(error.response.status);
      console.log(error.response.headers);
    } else if (error.request) {
      // The request was made but no response was received
      // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
      // http.ClientRequest in node.js
    } else {
      console.log("the error goes here");
      // Something happened in setting up the request that triggered an Error
      console.log("Error", error.message);
    }
    return { isAuth: false };

### Additional Library Versions :

"dependencies":` { "@expo/config-plugins": "~7.2.2", "@react-native-async-storage/async-storage": "1.18.2", "@react-native-masked-view/masked-view": "0.2.9", "@react-navigation/bottom-tabs": "^6.5.8", "@react-navigation/native": "^6.1.7", "@react-navigation/native-stack": "^6.9.13", "@react-three/fiber": "^8.13.7", "@reduxjs/toolkit": "^1.9.5", "axios": "1.3.4", "babel-plugin-react-native-classname-to-dynamic-style": "^0.22.0", "babel-plugin-react-native-classname-to-style": "^1.2.2", "babel-plugin-react-native-platform-specific-extensions": "^1.1.1", "chroma-js": "^2.4.2", "expo": "~49.0.11", "expo-asset": "~8.10.1", "expo-checkbox": "~2.4.0", "expo-constants": "~14.4.2", "expo-device": "~5.4.0", "expo-exotic-asset": "^1.0.4", "expo-file-system": "~15.4.4", "expo-gl": "~13.0.1", "expo-linear-gradient": "~12.3.0", "expo-media-library": "~15.4.1", "expo-notifications": "~0.20.1", "expo-splash-screen": "~0.20.5", "expo-status-bar": "~1.6.0", "expo-system-ui": "~2.4.0", "expo-three": "^7.0.0", "lottie-react-native": "5.1.6", "metro-react-native-babel-transformer": "^0.77.0", "react": "18.2.0", "react-dom": "18.2.0", "react-native": "0.72.4", "react-native-animatable": "^1.3.3", "react-native-calendars": "^1.1300.0", "react-native-date-picker": "^4.3.3", "react-native-dropdown-picker": "^5.4.6", "react-native-extended-stylesheet": "^0.12.0", "react-native-form-component": "^2.6.12", "react-native-gesture-handler": "~2.12.0", "react-native-keyboard-aware-scroll-view": "^0.9.5", "react-native-reanimated": "~3.3.0", "react-native-safe-area-context": "4.6.3", "react-native-screens": "~3.22.0", "react-native-stylesheet-plus": "^0.3.0", "react-native-svg": "13.9.0", "react-native-view-shot": "3.7.0", "react-native-web": "~0.19.6", "react-redux": "^8.1.2", "three": "^0.145.0", "expo-build-properties": "~0.8.3" }

React Native Version

0.72.4

Output of npx react-native info

System:
OS: Windows 10 10.0.19045
CPU: (8) x64 11th Gen Intel(R) Core™ i7-1165G7 @ 2.80GHz
Memory: 7.15 GB / 15.77 GB
Binaries:
Node:
version: 18.16.0
path: C:\Program Files\nodejs\node.EXE
Yarn:
version: 1.22.19
path: C:\Program Files\nodejs\yarn.CMD
npm:
version: 9.5.1
path: C:\Program Files\nodejs\npm.CMD
Watchman: Not Found
SDKs:
Android SDK: Not Found
Windows SDK:
AllowAllTrustedApps: Disabled
IDEs:
Android Studio: Not Found
Visual Studio: Not Found
Languages:
Java: Not Found
Ruby: Not Found
npmPackages:
@react-native-community/cli”: Not Found
react:
installed: 18.2.0
wanted: 18.2.0
react-native:
installed: 0.72.4
wanted: 0.72.4
react-native-windows: Not Found
npmGlobalPackages:
react-native”: Not Found
Android:
hermesEnabled: true
newArchEnabled: false
iOS:
hermesEnabled: Not found
newArchEnabled: Not found

Steps to reproduce

none

Snack, screenshot, or link to a repository

Snack

Hi.

I also just recently struggled with the same issue and I thought it was axios but when I used fetch(), I still encountered the same problem.

The issue here is that your API endpoint is set to “localhost”, use your machine IP address instead and that should fix your problem.