question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Axios post request network error on android

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:9
  • Comments:21

github_iconTop GitHub Comments

18reactions
mlazaricommented, Aug 23, 2022

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

5reactions
hemandpvkdcommented, Jun 29, 2022

Downgrade axios version to 0.24.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Axios post request network error on android - Stack Overflow
For me i am getting axios error for my emulator and did not getting any error for ios simulator. The problem ...
Read more >
React Native [AxiosError: Network Error] HTTP Fetching iOS ...
React Native cannot fetching using HTTP, so need HTTPS for ... React Native [ AxiosError : Network Error ] HTTP Fetching iOS and...
Read more >
Axios gives Network Error on android 6 : r/reactnative - Reddit
So I'm getting an error while making a API call using Axios. I have multiple scenarios: If I use https then it won't...
Read more >
Axios GET request throws network error in Android emulator ...
I am new to React-native. I am trying to perform a simple get request using the following code: axios.get('http://push-cycling-backend.dev/api/test') ...
Read more >
Api requests do not work on Android ( Network Error )
SDK Version: 40.0.0 Platforms: Android Api requests do not work on Android, I used axios and fetch for api requests. I'm getting an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found