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: open
  • Created 2 years ago
  • Reactions: 11
  • Comments: 29

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).

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