New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't post FormData since Axios 0.25.0 #4406
Comments
I'm having the same problem since updating to 0.25.0. |
We have the same issue, with the following code:
The backend throws a |
Same behaviour with same context (React Native), but with I have a bad request 400 error. Sample : const url = `https://XXXX.com/XXXX/token`;
const params = new URLSearchParams({
client_id: payload.client_id,
code: payload.code,
[...]
});
return await axios.post(url, params, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}); |
This problem appears to be due to someone recklessly optimizing the |
Same issue here, could the PR be merged and released, please. |
Why are these kind of useless "optimizations" even done? Why change something that works? Nobody will see any speed improvement. |
Thx bro <3. |
Same issue here, could the PR be merged and released, please. |
Merged, I will release tonight closing |
Any news about the next release @jasonsaayman? |
@TomCorvus I believe #4448 should be reviewed and merged first |
if you are having any issues with multipart form data upload on react native. |
0.26.0 does not work with React Native. Could someone please test the changes with RN before releasing? |
@tapz 0.26.0 Doesn't work for me too. Downgraded to 0.24.0 and multipart/form-data is working again! |
This issue is due to no boundary in the 'Content-Type'. Downgrading to 0.24.0 adds this automatically. |
It works for me: const formData = new FormData();
...
const response = await networking.post('/URL', formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
transformRequest: (data, headers) => {
return formData;
},
}); Versions: |
@kenikori is it the official way to post FormData with Axios now? Or is it just a crutch? |
If you read the docs they say there is no need to add custom content type from our end we need to use a method provided by axios, which will handle the default headers for api calls. |
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. |
Thanks @kenikori |
Just found out, thanks to this comment: that you don't need to return "formData" in your transformRequest, it is just enough to return "data". const formData = new FormData();
...
const response = await networking.post('/URL', formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
transformRequest: (data) => {
return data; // thats enough
},
}); |
Thx bro <3. |
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 |
@Wexleron please give the latest version a try and let me know if that solves this |
@jasonsaayman Hi, schteff's tip worked for me. Simply updating the Axios did not. Thanks to Schteff. The tip:
|
@Wexleron Can you please run the following code on your platform and then post the result here? import axios from "axios";
import utils from "axios/lib/utils.js";
const form = new FormData();
const prototype = FormData.prototype;
console.log(
JSON.stringify({
VERSION: axios.VERSION,
client: typeof navigator !== "undefined" && navigator.userAgent,
node: typeof process !== "undefined" && process.version,
isFormData: utils.isFormData(form),
toString: String(form),
stringTag: form[Symbol.toStringTag],
constructor: prototype.constructor,
props: Object.getOwnPropertyNames(prototype)
})
); |
@DigitalBrainJS I think he is using react-native. Which doesn't have navigator global. @Wexleron can you also provide info about the environment? Are you trying to run axios on react native or node or browser ? What are the versions, of react native, node, browser, etc? |
@kocyigityunus Axios uses this kind of check internally (https://stackoverflow.com/questions/39468022/how-do-i-know-if-my-code-is-running-as-react-native) besides React Native must emulate the browser environment, so the main globals should be present. |
React Native 17.0.1, Expo 5.4.3, node 16.14.2, running on Android 11 Device. It works now. I am sorry if I am not helpful, I am pretty much in learning process still. Also I am on react native as my secondary skill, I primarily focus on Python & Django backend.. |
I've tested |
Please donrt use 0.27.0 that was a borked release, please use the latest version |
none of above solutions works in axios@0.27.2 with "react-native": "0.68.2", :-( |
I've tested |
Just tested |
in my case it as an ID10T error... //now it worky as 'image' ENV |
I 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: |
Thanks for the solution. I follow that code, and i can throw formData to backend. env: |
Still having this error with 1.3.3. I need to still use this workaround. axios.post(url, formData, {
transformRequest: () => formData,
}) |
I am having the error with 1.3.4 as well. |
Solved ? |
dont think so . I still use 0.24.0
|
I tried with 1.4.0 but still have to use that workaround to make it work -> axios.post(url, formData, { |
I am using Axios 1.4 (also tried with 1.3.4) and the work around still does not work for me: If I look at the actual request payload it still just sends the string "[object Object]" instead of the actual data... help...
|
For anyone using nextjs 13 beta, axios 1.4.0 CLIENT: SERVER: |
convert the data into string (for example using JSON.stringify()) or blob before appending to formdata object |
@Enliven26 make sure to be using the right request headers |
I just switched from
|
It seems the #6056 breaks the |
We still require the |
Describe the issue
I can't post FormData since
"axios": "^0.25.0",
Error: Request failed with status code 400
Example Code
Expected behavior, if applicable
That it works
Environment
Additional context/Screenshots
No problem with
"axios": "^0.24.0",
I saw this PR #3757 on 0.25.0, but I don't know what I have to change in code.
Thanks for your help 👍
The text was updated successfully, but these errors were encountered: