node.jstypescriptaxiosmultipartform-dataform-data

How do you send FormData with Axios?


I tried the following from Node.js:

import axios from 'axios';

const axiosInstance = axios.create({});

(async () => {
    const sendData = new FormData();
    sendData.append('firmware', 'hello');

    const result = await axiosInstance({
        data: sendData,
        headers: { 'content-type': 'multipart/form-data' },
        method: 'PUT',
        url: 'http://localhost',
    });

    console.log(result);
})().catch(console.error);

But I got this error when calling send:

AxiosError: Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream

I am using axios version ^0.27.2. I tried various ways of stringifying the data, but I kept getting this error. I also tried using the form-data package, but its API does not match the actual FormData API and the data would not actually get sent.

How can I properly send my file as part of a FormData object to my endpoint?


Solution

  • As @robertklep pointed out, my axios version was old. Updating to the latest (currently ^1.11.0) solved my problem.