Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.4K
Vistas
Error: Data after transformation must be a string, an Array Buffer, a Buffer, or a Stream

I am getting this error while using POST method with axios and formidable. I am trying to upload a file to an API URL. Here is my code snippet.

var server = http.createServer(function(req, response) {
var filePath = '.';
filePath += req.url;


var mimeType = {
    '.ico': 'image/x-icon',
    '.html': 'text/html',
    '.js': 'text/javascript',
    '.json': 'application/json',
    '.css': 'text/css',
    '.png': 'image/png',
    '.jpg': 'image/jpeg',
    '.wav': 'audio/wav',
    '.mp3': 'audio/mpeg',
    '.svg': 'image/svg+xml',
    '.pdf': 'application/pdf',
    '.doc': 'application/msword',
    '.eot': 'appliaction/vnd.ms-fontobject',
    '.ttf': 'aplication/font-sfnt'
};

if (filePath == './') {
    filePath += "templates/index.html"
}

if (filePath == './success') {
    filePath = "templates/success.html"
}
var postUrl = "https://example.com/345678934/attachments";
const form = formidable({ multiples: true , keepExtensions : true});
const option = {
    method:'post',
    url: postUrl,
    headers : {
        'content-type': 'application/json'
    }
}

axios.post(option, function(err, fields,files) {
    console.log("error from post: ",err)
    //response.writeHead(200, {'content-type': 'application/json'});
    response.end(JSON.stringify({ fields, files }, null, 2));
    console.log("Upload completed");
    console.log(files);


}).then(res => {
    console.log(`statusCode: ${res.status}`)
    console.log(res)
  })
  .catch(error => {
    console.error("error from catch",error)
  })

fs.readFile(filePath, function(error, content) {
    if (error) {
        if (error.code == 'ENOENT') {
            response.writeHead(404);
            response.end('Page not Found');
        } else {
            response.writeHead(500);
            response.end('Internal error: ' + error.code);
        }
    } else {
        var extname = path.extname(filePath);
        response.writeHead(200, {
            'Content-Type': mimeType[extname] || 'text/plain'
        });
        response.end(content, 'utf-8');
    }
});

});

server.on('listening', function(err) {
    if (err) throw err;
    console.log(Date() + ' Server is running...');
});

server.listen(7000);

Here I am expecting to upload all types of file extension and multiple files also can be uploaded to that API URL.

Here it's throwing an exception "error from catch Error: Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream"

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I had a similar error using FormData on NestJS Application, I don't know if this helps you but to solve my problem i did the following changes:

import * as FormData from 'form-data';
import { Readable } from 'stream';

const form = new FormData();
form.append('file', Readable.from(file.buffer), {
      filename: file.originalname,
    })

credits to: https://stackoverflow.com/a/70660823/12216924

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda