Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda