axios: Can't post FormData since Axios 0.25.0
Describe the issue
I can’t post FormData since "axios": "^0.25.0",
Error: Request failed with status code 400
Example Code
export const client = axios.create({
baseURL: URL_API,
withCredentials: true,
responseType: 'json',
timeout: 30000,
});
const params = new FormData();
params.append('name', data.model);
params.append('idFuel', data.idFuel);
params.append('power', data.idPower);
client.post(`/app/society/${idSociety}/vehicle`, params, { headers: { "Content-Type": "multipart/form-data" } })
Expected behavior, if applicable
That it works
Environment
- Axios Version: 0.25.0
- Node.js Version: v14.17.6
- OS: iOS 15, Android 11 (But on all platforms and versions)
- Additional Library Versions: React Native 0.64.3
Additional context/Screenshots
No problem with "axios": "^0.24.0",
I saw this PR https://github.com/axios/axios/pull/3757 on 0.25.0, but I don’t know what I have to change in code.
Thanks for your help 👍
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 34
- Comments: 58 (4 by maintainers)
It works for me:
Versions: react-native: 0.67.2 axios: 0.26.0
This is a shorter form, since it is still needed in 0.26.1
The headers value might only be needed if you have modified the default Content-Type for axios. (just a guess since I have read that axios should set the correct Content-Type based on the FormData type)
In which axios version will the fix that closed this issue be released?
Just found out, thanks to this comment: https://stackoverflow.com/questions/56235286/react-native-post-form-data-with-object-and-file-in-it-using-axios/71116822#71116822
that you don’t need to return “formData” in your transformRequest, it is just enough to return “data”.
This problem appears to be due to someone recklessly optimizing the
isFormData
function in the utils.js file. This could break form detection logic in a non-browser environment where FormData is not a native object that can be recognized by theObject.prototype.toString
method. Fixed in #4413 with some additional benefits.We have the same issue, with the following code:
The backend throws a
Multipart: Boundary not found
errorI have added transformRequest according to @Wexleron answer. But i have still can’t fix this issue. But after quite a while I have found a solution to fix this issue, the key is don’t set “Conten-type: multipart/form-data” manually, just remove it, let axios create that header automatically. The folowing code will fix this issue :
Env: “axios”: “^1.1.3” “react”: “18.2.0”
Merged, I will release tonight closing
0.26.0 does not work with React Native. Could someone please test the changes with RN before releasing?
Why are these kind of useless “optimizations” even done? Why change something that works? Nobody will see any speed improvement.
React Native now works as of Axios
0.27.0
! No more inexplicable400
errors.I’m not going to add any bubble gum code to fix a bug in axios. I’ll just wait for a version that actually works. Not even sure if there is any need to upgrade 0.24.0 any time soon as this version works just fine.
Any news about the next release @jasonsaayman?
Thx bro ❤️. I have the same issue here, and just downgrading to “^0.24.0” version, solved my problem. Waiting for the release.
Works perfectly for me
if you are having any issues with multipart form data upload on react native. please check this answer.
Just tested
0.27.2
and it’s working as expected now.Axios 0.26.1 This bug still occurs… Env:
@kenikori is it the official way to post FormData with Axios now? Or is it just a crutch?
Still having this error with 1.3.3. I need to still use this workaround.
none of above solutions works in axios@0.27.2 with “react-native”: “0.68.2”, 😦
Please donrt use 0.27.0 that was a borked release, please use the latest version
I’ve tested
0.27.2
today in React Native and found it works, where it previously wasn’t.I’m having the same problem since updating to 0.25.0.
convert the data into string (for example using JSON.stringify()) or blob before appending to formdata object
dont think so . I still use 0.24.0
I’ve tested
0.27.2
with “react-native”: “0.67.4” and I can confirm this works, without the need to use “transformRequest”@jasonsaayman Hi, schteff’s tip worked for me. Simply updating the Axios did not.
Thanks to Schteff.
The tip:
2 days for me, then i give up for 2 weeks and take a rest:-) I did exactly by the book, every help on internet was exactly same and didnt functioned. I still ahve problems. but at least I know wheres the catch