Search code examples
javascriptnode.jsaxiosfs

Axios: data should be a string, Buffer or Uint8Array


I'm getting this error when trying to do a POST request using axios:

TypeError: data should be a string, Buffer or Uint8Array

Here is my code snippet:

var fs = require('fs'),
axios = require('axios');

var FormData = require('form-data');
var form = new FormData();
form.append('file', fs.createReadStream("qa_test_file_DOC.txlf"));
form.append('extractArchive', false);


let request_config = {
    headers: {
    'Authorization': `Bearer eyJhbGciOiJIUzI1NXXXX.....`,
    ...form.getHeaders()
 }
}

let reqUrl = "https://XXXXX/XX/rest/v1/XXXXX";
try {
    axios.post(reqUrl, form, request_config)
        .then(function (response) {
        console.log(response);
        return callback(response);
     })
    .catch(function (error) {
        console.log(error);
        return callback(error);
    });
} catch (ex) {
  console.log("exception   ", ex);
}

Tried using pipe, and most of possible solutions. file is exist. Not understanding what going wrong here. anything in Readstream ? Thanks for help.


Solution

  • After spending much time and tried lots of possible things, I observed error that I am getting is.

    TypeError: data should be a string, Buffer or Uint8Array
    

    and in my formData I am appending one more variable with file is

    form.append('extractArchive', false);
    

    This is nothing but boolean and axios or formData is giving error for this. I changed it to,

    form.append('extractArchive', 'false');
    

    That solved my issue. may it will help if someone running is such problem.

    Thanks for your help.