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

175
Vistas
Parse Multipart Data and write images to files

I am trying to parse an API response from a RETS server that is image data in a multipart format. I am trying to manually parse the data using the code below and write the image data to files, but when I go to open the image file I get a message saying that the file type is unsupported. Any help I can get is greatly appreciated.

Code to make the API call and parse the data:

let image_res = await axios.get(url, 
        {
            // responseType: 'arraybuffer',
            headers: {
                'Accept': '*/*',
                'Authorization': 'Bearer ' + token,
                'Connection': 'keep-alive',
                'Content-Type': 'application/x-www-form-urlencoded',
                'Accept-Encoding': 'gzip, deflate, br'
            }
        }
    )

    var boundary = image_res.headers['content-type'].split(';')[1].split('=')[1];
    let parts = image_res.data.split(boundary)
    

    for (var i = 0; i < parts.length; i++) {
        var dirty_img = parts[i].split('Content-Type: image/jpeg')[1]
        if (dirty_img) {
            var img = dirty_img.trim();
            img = img.replace(/--/g, '');

            console.log("Image: ", img)
            fs.writeFile(`./LongLeaf/images/556354089-${i}.jpg`, img, { encoding: 'binary', flag: 'w+' }, err => {
                if (err) {
                    console.log(err)
                    return
                }
            })
        }

    }

Screenshot of the data returned from the API call: enter image description here

This pattern continues throughout the data where each image will have a separator similar to --TRM44afa5fc8b3d4651b3205064a794c38a and then Content-ID, Object-ID, Order-Hint, Content-Type, a blank line, and then the image data.

I tried using the parse-multipart-data npm package but it was returning an empty array each time. I initially thought the issue was that there was no filename for the images but I wrote code to add in a filename and still get an empty array.

about 4 years ago · Juan Pablo Isaza
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