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.

Support other FormData implementations

See original GitHub issue

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
node
const 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: { [...]

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
verhovskycommented, Apr 28, 2022

Node 18 was just released and it comes with a native FormData and that also should be supported. This is what I get with axios 0.27.2

$ node
Welcome to Node.js v18.0.0.
Type ".help" for more information.
> const axios = require('axios');
undefined
> await axios.post('localhost:8888', new FormData())
(node:24059) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Uncaught:
[AxiosError: Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream] {
  code: 'ERR_BAD_REQUEST',
  config: {
3reactions
DigitalBrainJScommented, Apr 27, 2022

I was going to add support for it (both form-data and formdata-node) when I did a PR with the FormData enhancement for Axios about six months ago, but then I decided to keep only form-data for 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-node simultaneously)

import axios from "axios";
import { Readable } from "stream";
import { FormDataEncoder } from "form-data-encoder";
import { FormData } from "formdata-node";
import FormDataLegacy from "form-data";

((transforms) => {
  const isFormData = (thing) =>
    thing &&
    Object.prototype.toString.call(thing) === "[object FormData]" &&
    thing.values;

  transforms.unshift((data, headers) => {
    if (isFormData(data)) {
      const encoder = new FormDataEncoder(data);
      Object.assign(headers, encoder.headers);
      return Readable.from(encoder);
    }

    return data;
  });
})(axios.defaults.transformRequest);

// use axios with it
(async()=>{
  function test(form) {
    form.append("test", 123);
    return axios.post("https://httpbin.org/post", form).then(({data})=> console.log(data), console.warn);
  }

 console.log(axios.VERSION);
 // formdata-node
 await test(new FormData());
 // formdata
 await test(new FormDataLegacy());
})();

Read more comments on GitHub >

github_iconTop Results From Across the Web

FormData - Web APIs | MDN
Chrome Edge FormData Full support. Chrome5. Toggle history Full support. Edge12. Toggl... @@iterator Full support. Chrome50. Toggle history Full support. Edge18. Toggl... FormData() constructor Full support....
Read more >
FormData
FormData objects are used to capture HTML form and submit it using fetch or another network method. We can either create new FormData(form)...
Read more >
REST Up: Consuming a Multipart/Form-Data REST Method ...
In this blog post, OutSystems MVP Killian Hekhuis explores what a multipart/form-data is and how to consume it when using a low-code ...
Read more >
form-data
var FormData = require('form-data'); var http = require('http'); var form = new FormData(); http.request('http://nodejs.org/images/logo.png', ...
Read more >
How to use FormData in node.js without Browser?
6 Answers 6 · 2. Thanks for your answer. · If its node js, you only can do var FormData = require('form-data'); Node...
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