Sorry in advance for the possible bad implementation, I just started studying javascript and Node after 10 years without coding.I did this with the use of chatgpt.
I'm trying to figure out how to properly write a zip file from a base64 payload received from a REST call.
request(options, function (error, response) {
if (error) throw new Error(error);
let testPath = folder + '/' + ts ;
fs.mkdirSync(testPath, (err) => {
if (err) {
console.error('Error creating folder:', err);
} else {
console.log('Folder created successfully!');
}
});
fs.writeFileSync(testPath + '/response.xml', response.body, (err) => {
if (err) {
console.error('Error saving response to XML file:', err);
return;
}
console.log('Response saved to response.xml');
});
let parser = xml2js.Parser();
parser.parseString(response.body, (parseErr, result) => {
if (parseErr) {
console.error("Error parsing response:", parseErr);
return;
}
let parsedResult = result;
const payloadString = parsedResult.response.payload[0];
console.log('payload is: ' + payloadString)
// Decode the base64 payload
const decodedPayload = Buffer.from(payloadString, 'base64');
console.log('decoded is: ' + decodedPayload)
fs.writeFileSync(testPath + '/payload.zip', decodedPayload, (err) => {
if (err) {
console.error('Error saving payload to ZIP file:', err);
return;
}
console.log('Payload saved to ' + testPath + '/payload.zip');
});
// Add logging for debugging
//console.log('Decoded Payload:', decodedPayload.toString()); // Log the decoded payload before writing to the zip file
// Your existing code...
console.log('Job_ID: ' + parsedResult.response.jobInfo[0].$.id +' for file ' + zipFilePath + ' and received a ' + response.statusCode); //logga job id , file usato e status code
});
});
this is what I get when I try to unzip the file:
diego@DESKTOP:/mnt/c/Users/diego/path/$ unzip payload.zip
Archive: payload.zip
caution: zipfile comment truncated
error [payload.zip]: missing 4030336493 bytes in zipfile
(attempting to process anyway)
error [payload.zip]: attempt to seek before beginning of zipfile
(please check that you have transferred or created the zipfile in the
appropriate BINARY mode and that you have compiled UnZip properly)
I tried logging everything to the console, i tried aswell to decode manually the base64 payload from base64guru and it returns a correct zip that I can access from my computer.
I also tried asking chatgpt but keeps telling me the same things and after trying over and over I'm stuck.
Thanks, Diego