2

So i'm posting a formdata object with axios to a node.js server. On iOS everything works perfectly, the data get posted and the image uploaded. But on android i'm getting this error

[AxiosError: Network Error]

here's my axios call

const handleSubmit = async (listing, { resetForm }) => {
    
    const data = new FormData();
listing.images.forEach((image, index) =>
      data.append("images", {
        name: `product${Math.floor(Math.random() * 1000)}.`,
        uri: image,
      })
    );

    const res = await axios
      .post("http://192.168.43.8:5000/products/addProduct", data, {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      //tried adding this but didn't work out
        transformRequest: (data) => data,
      })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
        // handle error
      });
  }


}

Please note that on iOS it works without a problem. here's a screenshot of the error when i used the react native debugger

enter image description here

Sb Zakaria
  • 311
  • 6
  • 18

3 Answers3

1

if you use android emulator you need to change your ip to 10.0.2.2

change this: http://192.168.43.8:5000/products/addProduct

to this: http://10.0.2.2:5000/products/addProduct

Source: https://github.com/axios/axios/issues/973#issuecomment-437221047

TOPKAT
  • 6,667
  • 2
  • 44
  • 72
0

By default http calls are blocked from android 9 version onwards, you either need to make your api calls HTTPS or you need to explicitly allow connection to this IP in your manifest file. Please refer this SO thread. How to allow all Network connection types HTTP and HTTPS in Android (9) Pie?

Vilsad P P
  • 1,529
  • 14
  • 23
  • do i need , since i'm using expo, to create AndroidManifest.xml file in my app root folder? – Sb Zakaria Jun 29 '22 at 16:56
  • you can use the expo's way of managing AndroidManifest file. Once App is built and installed in android, you will need this in the manifest to make this work. check the below for reference. https://chafikgharbi.com/expo-android-manifest/ – Vilsad P P Jun 29 '22 at 17:03
0

For me i am getting axios error for my emulator and did not getting any error for ios simulator. The problem with my emulator is that it is not able to connect to Internet.

So I added google DNS server 8.8.8.8 (you can add any DNS server) for my mac and it worked.

jeet
  • 107
  • 2
  • 17