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
Easier way to work with FormData #3721
Comments
Yes, a agree with the issue and have same problem - in some my projects I have need to serializу body of all requests. For I prepare a Pull Request for Axios repository for adding new feature in Axios library. Also it will could use for GraphQL body serializing mayby. |
Thanks @alexbidenko1998, |
@carpben , I understand your idea and I need same function for Axios, but I assumed that for universaly library like Axios more powerfull would be method for body converting, like paramsSerializer. It could using for different pupose. You also can use it for your problem. And I had solution in my project and proposed it for Axios library) P.S. This issue I use for Pull request becouse I think that you write about same problem like me. |
Hello everyone. I am ready to contribute. But probably the contract and requirements should be determined first:
Also, it would be good to get a confirmation from the maintainers that they are ready to include this new feature in axios. |
#3757 adds a function to convert a data object (plain old JS object) to FormData. |
See https://github.com/axios/axios#using-applicationx-www-form-urlencoded-format. axios expects users to handle this kind of work by special packages. (cc @jasonsaayman) |
@chinesedfan , I'm not suggesting to change the current API or functionality in any way. Since usage of FormData is done only in the context of a requests/calls, it makes sense to export such a function from the library itself. This might looks as such: import axios, {toFormData} from "axios"
axios({
method: "post",
url: "www.example.com/endpoint",
data: toFormData(jsObject),
headers: { "Content-Type": "multipart/form-data" },
}) If a project makes use of Axios for it's AJAX calls, it will only build FormData where axios is used, so this could be very convenient for users. |
Hello! 👋 This issue is being automatically marked as stale because it has not been updated in a while. Please confirm that the issue is still present and reproducible. If no updates or new comments are received the issue will be closed in a few days. Thanks. |
Feature suggestion still relevant. |
Hello! 👋 This issue is being automatically marked as stale because it has not been updated in a while. Please confirm that the issue is still present and reproducible. If no updates or new comments are received the issue will be closed in a few days. Thanks. |
Still relevant, adding additional statuses |
Hello! 👋 This issue is being automatically marked as stale because it has not been updated in a while. Please confirm that the issue is still present and reproducible. If no updates or new comments are received the issue will be closed in a few days. Thanks. |
Hello! 👋 This issue is being automatically marked as stale because it has not been updated in a while. Please confirm that the issue is still present and reproducible. If no updates or new comments are received the issue will be closed in a few days. Thanks. |
Hello! 👋 This issue is being automatically marked as stale because it has not been updated in a while. Please confirm that the issue is still present and reproducible. If no updates or new comments are received the issue will be closed in a few days. Thanks. |
@jasonsaayman what can we do to advance this feature suggestion? |
@carpben It's true that will be a little more convenient for users if axios does inside, but increase more burden for axios maintainers. That's a tradeoff and I admit that I am conservative. In fact, users can do this simple things very well. If you don't like to create form data everywhere, axios interceptors can help you. Compared with other request client, i.e. sindresorhus/ky, users also need create FormData or URLSearchParams by themselves. So I will persist in the original opinion and open to listen new discussions from you and @jasonsaayman. |
@chinesedfan , I'm not saying that creating form data is complex, it isn't.
|
Hello! 👋 This issue is being automatically marked as stale because it has not been updated in a while. Please confirm that the issue is still present and reproducible. If no updates or new comments are received the issue will be closed in a few days. Thanks. |
Nesting formdata is a nightmare. I'm still unable to do it. It took me a while to even realize what the problem was. |
So we need to let axios know that parsing data is literally instance of FormData,l right? |
Is your feature request related to a problem? Please describe.
Yes.
When fetching and sending data as form data, the user is required to manually build a form data object.
Describe the solution you'd like
Provide a reliable easy way to convert object to form data.
Describe alternatives you've considered
I couldn't find a simple existing solution to easily convert a nested object to form data.
Additional context
Since form data is only used (as far as I know) in the context of fetching, I thought Axios should provide this functionality. Perhaps export a function that can convert an object to form data, or add a property to AxiosConfig and handle the conversion internally.
If relevant I'd be happy to submit a PR.
The text was updated successfully, but these errors were encountered: