Skip to content

Using FormData type from Node.js 18 global leads to an error. #5327

Closed

Description

@olexandr-mazepa

Describe the bug

When the request data value is an instance of the Node.js 18 FormData class the Axios throws the error with the next message.
Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream
Seems like the error goes from this part of the code:

axios/lib/adapters/http.js

Lines 236 to 251 in d032edd

if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {
headers.set(data.getHeaders());
} else if (data && !utils.isStream(data)) {
if (Buffer.isBuffer(data)) {
// Nothing to do...
} else if (utils.isArrayBuffer(data)) {
data = Buffer.from(new Uint8Array(data));
} else if (utils.isString(data)) {
data = Buffer.from(data, 'utf-8');
} else {
return reject(new AxiosError(
'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
AxiosError.ERR_BAD_REQUEST,
config
));
}
.
I suppose that current checks don't expect FormData object that goes not from either Browser or node-fetch package but from FormData type added to Node.js 18.

To Reproduce

const axios = require('axios');

(async () => {
  const axiosInstance = axios.create({});
  const FormData = global.FormData;
  const data = new FormData();
  data.append('somePropety', 'someValue');
  try {
    const response = await axiosInstance.request({
      method: 'POST',
      url: 'http://test.com',
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      data,
    });
    console.log(response);
  } catch (error) {
    console.error(error);
  }
})();

image

Code snippet

No response

Expected behavior

Support of Node.js FormData added to Axios

Axios Version

1.2.0

Adapter Version

No response

Browser

No response

Browser Version

No response

Node.js Version

v18.12.1

OS

macOs 12.6.1

Additional Library Versions

No response

Additional context/Screenshots

No response

Activity

  1. gbersac commented on Apr 14, 2023

    @gbersac

    I had this issue using "axios": "^1.3.5"

  2. DigitalBrainJS commented on Apr 14, 2023

    @DigitalBrainJS
    Collaborator

    @gbersac Could you share a minimal reproducible example in a fork of this sandbox?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions