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:

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.