Home > database >  Axios post request network error on android
Axios post request network error on android

Time:06-30

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

CodePudding user response:

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

CodePudding user response:

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?

  • Related