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.

FromData Axios and react native (web), inconsistent?

See original GitHub issue

Describe the issue

I’m trying to post a file using axios on react native (web):

Example Code

	const form = new FormData()
	form.append('image', image, 'image.jpg') // <-- Blob with mimetype 'image/jpeg'
	axios.patch(`/image`, form, {headers:{"Content-type":"multipart/form-data"}})

Expected behavior, if applicable

When using react native web everything works fine, but when I’m using react native mobile the backend returns:

multipart file(s) required {"errors":[{"message":"multipart file(s) required","path":"/image"}],"name":"Bad Request","status":400}

What’s going on here? Why is the behavior different between web and mobile?

Environment

  • Axios Version: ^0.25.0
  • Browser: firefox
  • React native version: 0.64.3
  • React native web version: 0.17.1

Additional context/Screenshots

no additional content

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:9
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
kamilwezgowieccommented, Mar 9, 2022

Adding transformRequest will fix the issue, as mentioned here https://github.com/axios/axios/issues/4406#issuecomment-1048693170

const formData = new FormData();
...
const response = await networking.post('/URL', formData, {
  headers: {
    'Content-Type': 'multipart/form-data',
  },
  transformRequest: (data, headers) => {
    return formData;
  },
});
3reactions
hirbodcommented, Mar 4, 2022

FormData is broken since Axios 0.25. It is working perfectly fine with 0.24 on react-native. I wasted 4 hours until I’ve found a similar issue which told me to downgrade.

There are also StackOverflow issues. https://stackoverflow.com/questions/71321756/react-native-expo-axios-file-upload-not-working-because-axios-is-not-sending/71323380

And this issue (with a workaround): https://github.com/axios/axios/issues/4406

Version 0.24 Bildschirmfoto 2022-03-04 um 01 08 07

Version 0.25 and Version 0.26 Bildschirmfoto 2022-03-03 um 22 13 20

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem using Axios FormData in React-Native - Stack Overflow
I'm working with a Recat-Native frontend and a Django backend. It al started with that I tried to set up a Axios Formdata...
Read more >
User submits with inconsistent behaviour - Support
I am having some inconsistent behaviour when users submit data through my form. ... 'react-dropzone'; import axios from 'axios'; const addData = (object) ......
Read more >
Network error on body exceeding a certain size-Reactjs
Coding example for the question Axios XMLHttpRequest - Network error on body exceeding a certain ... let formData = new FormData(); formData.append("files", ...
Read more >
Killing CORS Preflight Requests on a React SPA
When I first saw them appearing in the web inspector, it confused me ... It is also unfortunate that preflight requests are incompatible...
Read more >
ReferenceError: can't access lexical declaration 'X' before ...
The JavaScript exception "can't access lexical declaration `variable' before initialization" occurs when a lexical variable was accessed ...
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