axios: Axios post request network error on android

Describe the bug

0

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. PLEASE NOTE THAT I DID READ THE PREVIOUS ISSUES AND TRIED THE SOLUTION. NOTHING WORKED !

To Reproduce

Code snippet to reproduce, ideally that will work by pasting into something like https://npm.runkit.com/axios, a hosted solution, or a repository that illustrates the issue. If your problem is not reproducible, please file under Support or Usage Question

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 Screen Shot 2022-06-29 at 5 26 14 PM

Expected behavior

FORMDATA OBJECT POSTED TO NODE.JS server

Environment

  • Axios Version 0.27.2

  • Adapter : HTTP

  • Browser : Chrome

  • Browser : Version 103.0.5060.53 (Official Build) (arm64)

  • Node.js : v14.17.5

  • OS: macOS big sur version 11.4

  • expo: “~45.0.0”

  • “react-native”: “0.68.2”,

Additional context/Screenshots

Add any other context about the problem here. If applicable, add screenshots to help explain.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 11
  • Comments: 30

Most upvoted comments

Ok, so after a few hours of debugging, I finally found what’s wrong - it’s the Content-Type request header. If you don’t set it explicitly, then the default value is set here to application/x-www-form-urlencoded -> https://github.com/axios/axios/blob/v0.27.2/lib/defaults/index.js#L10 and that apparently makes the request fail on Android, but not on iOS. Setting the 'Content-Type': 'multipart/form-data' explicitly fixes the error. It was working in 0.26.1 because this code was removing the Content-Type header: https://github.com/axios/axios/blob/v0.26.1/lib/adapters/xhr.js#L32

Downgrade axios version to 0.24.0

I am also getting the same error in android device & simulator Setting 'Content-Type': 'multipart/form-data' didin’t help. For me the error happens only for post request - with multipartf/orm-data Other GET & POST apis are working correctly. Funny thing is all api works correctly in iOS

@Mkay4real @singhgovind770 Did you try to set 'Content-Type': 'multipart/form-data' in headers when you make the request?

Can confirm setting 'Content-Type': 'multipart/form-data' makes it work again

Like its been mentioned above; Setting the 'Content-Type': 'multipart/form-data' explicitly fixes the error.

If it doesn’t, make sure the type on your Blob is a valid MimeType. In my case, I was passing type: 'image', in iOS, it worked fine, but on android, it didn’t work until I updated it to type: 'image/jpeg' accordingly.

@mlazari Thank you!!! ‘Content-Type’: ‘multipart/form-data’ fixed it for me!

I see the same issue starting with version 0.27.0 (Network Error on Android, works fine on iOS). It works fine on both platforms with 0.26.1 (with a workaround for another issue though - https://github.com/axios/axios/issues/4412#issuecomment-1025506556).

For me, it was not related to form data or anything frontend related! On Android only, I could not reach our production server. Axios gave an ERR_NETWORK error without more info. This was working on iOS! And dev/staging URLs were working perfectly fine on the same Android device.

I asked a friend to execute the same Axios request as in my app in his own Android app and he got the same ERR_NETWORK error so this meant it was not an issue specific to my Android app… Then I created the fetch version request of my Axios request. example:

await fetch('URL', {
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
  },
})
  .then(async response =>
    console.log(
      'TEST SUCCESS',
      JSON.stringify(response),
      await response.json(),
    ),
  )
  .catch(response => console.log('TEST ERROR', JSON.stringify(response)));

You can execute this request from any Javascript capable device to see where it does and does notwork. If it works on other devices, the issue is probably on your app side, if it doesn’t work from anywhere, it’s Backend related. In my case, it didn’t work from anywhere so it was an issue for the Backend dev’s.

I was getting the same error. Spent several days and a lot of time upgrading packages and trying all the solutions from issues and stackoverflow. Finally this was the mistake which I don’t think is mentioned which even in the docs.

If we have a FormData object

formData.append("name", "abc")
formData.append("state", undefined)
formData.append("address", {
    addressLine1: "",
    addressLine2: ""
});

It will throw the same error since the "state" is undefined and "address" is an object. But FormData accepts only string or File or Blob. So this was causing the issue for me. Once I replaced undefined with "" and "address" with JSON.stringify(addressObj) it started working.

Reference -> https://stackoverflow.com/a/72899049/5918233

just used fetch over axios working now

EDIT - built the app again with expo eas and again it stops working even with fetch

I have the same problem, but mine only gives an error when I use android 5.1.1