Reputation: 93
I am working on automating a new set of APIs and one issue encountered is that one of APIs is returning a base64 encoded file. eg:
var base64EncodedData=iVBORw0KGgoAAAANSUhEUgAAAMsAAAAyCAYAAADyZi/iAAADsElEQVR42u2aMWgUQRSGwxVBJI2E4wiSJsgRJNiIpDhEBLGQKyRgIUFCEIJISGUnFiIBkRQpJK1YhECwSiGBIFcEsZEQgkgaEREJEpBDRIIcrG9gDsdxbnd2s7u3d34fPEKSebM7M++fffN2BwYAAAAAAAAAAAAAAKDXCCwyukZJ7JbYutgnsZ9irayvC38W4GLgoMfGsGbdvgqisQT9nNG+JqtFEIt0OS62H0RARGcXZCfFPvSBWIbFvlpD2E7Qz7bVh+pzuNtike6G9JMkQCzdC7Llfpl0ueUbjmHMx/BfcPhPFSENk+7uW91/FLsiNkgU5xNcNWPym/2wQ+lc3uS72KiH35jYD8v3RVHOLNJdw+r+MhGcX1CdsNKv230ilrLYoTWULQ+/Lcvnm1ilQGKxhVwiivMLqiVj4ht5VXNyGtu0I52aDWl/x9F+ukjVMCpe3QumSWPej9pVo6wWW7q6pqtVB2K/dLVpV+xR2OFZ/ld3+O2JPREbibjmhuNJMeJoN6pTNZONhOPsOH/GHDSNseyILUaNpZ82sl4TyqBVfryXxoK4fHVK9CqieKNSpktWXxWx1xF+KvivhtzPiG4TKgJH+tX0Cd4Yc6CqdJsRY1FirUf1RyUsf7E8NuZ2N63dy7Fup8Teea6xCpaqEeRfPP3U7jwRck+zDp+bxv9n4qRrCeZAlXvfeo5FPW0mEUtxhHLBmFeVA5/NUCzrxnuK+fZurZ9sMzq1Mmnot9M7+vcDT783MQ/uh3q3dz15No85vzar+udnfS4q63bq3dacoxCxh1iKk369N+Z1Mc28uMPa7bcDxNG+qs9Lf5VqE/rVQu7LdSZZc5xpVJvTKYsl0E/XTmM5bx/c1dcUnFm6L5aHVhAPZiyWf55cDp+nHfzGI/xWLJ+liPZzHpvy3RTm2JVaVSN8nlk+y4ilu0I5Z+1gtbQXxBEoax4+dVfqksDvpYdPWKGhkdI82zz38JnySQURSz5CKVkH7ZUsypNJPhPRRYA0/JoePq439IH+21hGYrnu4VP2GQtiyT/9UlWmoZzEUvEUchp+R5736Pr2ayHFuc5sLIgle6FMWOlXPc5iHydQcvZrZX29bo8FsWSffu3FPEP0qliCPhJLgFjyF8sD6233MGJBLOCe3FbcjwIRC2L5X8WSOogFsSAWxIJYEAtiQSyQi8A4syAWQCyIBRALYgHEglgQC2JBLIgFsSAWxIJYEAtiAQAAAAAAAAAAAAAAAAAAAAAAAACAAvIbRNwB01NnU/AAAAAASUVORK5CYII=
I want to create an image file from this encoded file. Tried multiple way using 'atob' to convert the base64 to binary format. However, once I get the binary data in the form of byte array, I am unable to download it to my local to use it in the next request.
Logic 1: // Coverting the base64EncodedData to binary and logging on console.
atob(response.base64EncodedData)
console.log(atob(response.base64EncodedData));
// Logic for converting the base64 encoded data to byteArray.
var binaryData = atob(response.base64EncodedData);
var byteArray = new Uint8Array(binaryData.length);
for (var i = 0; i < binaryData.length; i++) {
byteArray[i] = binaryData.charCodeAt(i);
console.log(byteArray[i])
}
With Logic 1: I am able to Covert the base64EncodedData to binary and log it on console. However, yet unable to download it as a png file to my local. With Logic 2: I am able to convert the base64EncodedData to byte array However, still unable to convert it to file.
I used the below logic to covert it to file, yet unlucky:
const buffer = Buffer.from(byteArray);
const folderPath = 'C:/test/downloadedFile/';
const fileName = 'example.png';
const filePath = folderPath + fileName;
fs.writeFileSync(filePath, byteArray);
This throws TypeError: fs.writeFileSync is not a function
Could someone please help me out on this.
Upvotes: 0
Views: 774
Reputation: 7866
As per my knowledge this isn't directly possible in the Postman. Instead you can create a local custom web service/API, and hit that endpoint once get base64 response. The local API should be responsible to convert it to file and store it to the local file system.
Related Reference: Automate to convert base64 decoded value to PDF file and download it to pc
Alternate Solution-1: Manual Download
YouTube: Upload, Download, and Visualize Files in Postman
Alternate Solution-2: Visualizer
An Alternate way is to visualize an image or a file. This isn't useful in automation though.
Upvotes: -1