Repository navigation
Closed
Description
Is your feature request related to a problem? Please describe.
It's related to the problem of sending multipart/form-data with Axios on Node.js
Describe the solution you'd like
I would like to be able to do
const axios = require('axios');
const { FormData } = require('formdata-node');
const form = new FormData();
axios.post('http://localhost:8888', form)Describe alternatives you've considered
N/A
Additional context
got recommends using formdata-node. Axios recommends form-data.
I would like to use formdata-node with both because I want to use both got and Axios and I don't want people reading the code to need to notice that there's two different libraries used for FormData. Also formdata-node has a table saying that form-data's FormData doesn't have a bunch of methods that the browser's does. I tried using formdata-node with Axios
cd /tmp
mkdir axios-form-test
cd axios-form-test
npm init -y
npm install axios
npm install formdata-node
nodeconst axios = require('axios');
const { FormData } = require('formdata-node');
const form = new FormData();
form.append('my_field', 'my value');
form.append('my_other_field', 'my second value');
axios.post('http://localhost:8888', form)and got an error
Uncaught:
Error: Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream
at createError (/private/tmp/axios-test/node_modules/axios/lib/core/createError.js:16:15)
at dispatchHttpRequest (/private/tmp/axios-test/node_modules/axios/lib/adapters/http.js:98:23)
at new Promise (<anonymous>)
at httpAdapter (/private/tmp/axios-test/node_modules/axios/lib/adapters/http.js:48:10)
at dispatchRequest (/private/tmp/axios-test/node_modules/axios/lib/core/dispatchRequest.js:58:10)
at Axios.request (/private/tmp/axios-test/node_modules/axios/lib/core/Axios.js:108:15)
at Axios.<computed> [as post] (/private/tmp/axios-test/node_modules/axios/lib/core/Axios.js:140:17)
at Function.wrap [as post] (/private/tmp/axios-test/node_modules/axios/lib/helpers/bind.js:9:15) {
config: { [...]
Metadata
Metadata
Assignees
Labels
No labels
Activity
octet-stream commentedon Apr 27, 2022
I'd rather recommend to use
form-data-encoderpackage to do so. That way Axios will have support formultipart/form-dataencoding in general, regardless of what package you choose - either formdata-node, or formdata-polyfill, or something else (except for form-data package, because it's not spec-compliant).jasonsaayman commentedon Apr 27, 2022
Latest version of axios will handle this for you without any need of installing a package, please check the latest docs
verhovsky commentedon Apr 27, 2022
Where are the latest docs? I checked
https://github.com/axios/axios-docs/blob/master/posts/en/urlencoded.md#form-data
https://github.com/axios/axios-docs/blob/master/posts/en/req_config.md
And neither mention it. The latter doesn't even mention form-data, it just says
FormDatais browser only. I also noticed the former doesn't mention thatURLSearchParamscan be used on Node since Node 10, it just tells you to import it from a library.DigitalBrainJS commentedon Apr 27, 2022
I was going to add support for it (both
form-dataandformdata-node) when I did a PR with the FormData enhancement for Axios about six months ago, but then I decided to keep onlyform-datafor a while, since the package was pretty new. Most likely in the near future we should replace the default form-data module with formdata-node, or create our own simple stream encoder, but writing it in ES5 is painful.Currently, you can use a custom transform to support this package (both
form-data&formdata-nodesimultaneously)verhovsky commentedon Apr 28, 2022
Node 18 was just released and it comes with a native
FormDataand that also should be supported. This is what I get with axios 0.27.2octet-stream commentedon Apr 28, 2022
Hmm. I think if you implement Blob support in Axios for Node.js adapter it should be relatively simple, because you can just convert FormData to blob like this: https://github.com/jimmywarting/FormData/blob/master/formdata-to-blob.js
To support Blob, however, Axios need to be able to read both ReadableStream and Readable (in case if somebody will use
[email protected]) produced byBlob.prototype.stream()method, because other methods (.text()and.arrayBuffer()) would not be memory efficient.[-]Support formdata-node[/-][+]Support other FormData implementations[/+]jimmywarting commentedon Jul 6, 2022
It is relative straight forward to create a NodeJS stream using:
node:stream.Readable.from(blob.stream())if you still depend on that nodejs stuff. the fetch spec have recently changed to be acceptable of asyncIterator to support both whatwg & NodeJS streams, think you should support async iterators too...I think we could start with adding support for Blob's as an acceptable data payload...
On the other hand: if we switch the adapter to use built in fetch in NodeJS v18 or using Undici or node-fetch
then it would be relative simple to support both blobs and formdata and just let this slide in as it can handle it for you so that you do not have to encode formdata or do anything special to support blobs either...
using fetch have also the benefit to make it work in service worker too... and cloudflare/Deno
maybe ppl should be able to bring in there own fetch impl if they wishes. Maybe it's time to ditch the old
node:http&node:httpscore adapter module.Dany-C commentedon Nov 13, 2022
With Node 18, you can now use the native globals FormData and fetch.
Notices :
Below a simple exemple with 2 little text files
`
const url = 'insert-your-url-here';
const formData = new FormData();
const blobTxt1 = new Blob(['test text 1'], {type:'multipart/form-data'});
const blobTxt2 = new Blob(['test text 2'], {type:'multipart/form-data'});
formData.append(
'test txt 1',
blobTxt1,
'testTxt1.txt'
);
formData.append(
'test txt 2',
blobTxt2,
'testTxt2.txt'
);
const resultTestTxt = await fetch(url, {
method: 'POST',
body: formData,
});
`
Dany