This issue I'm having could be literally anything in my workflow, but I will start with this scope for now...
I have a rest api that you send a JSON structure and it sends back an excel file, but I can't get it to work probably in the browser/Javascript.
My Javascript code in the browser is basically the following:
fetch('myapiurl')
.then(response => response.blob)
.then(blob => downloadBlob(blob))
function downloadBlob(blob){
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = 'export.xlsx;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
But this generates a corrupted file, there's either something missing or I'm not sending the file in the right encoding.
Here's the python code from the API in the part where it sends the file (it's an AWS lambda):
output = BytesIO()
#Code that puts an excel file in output
return {
'statusCode' : 200,
'headers' : {
'Content-Disposition': 'attachment; filename="export.xlsx"',
'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'Access-Control-Allow-Origin': '*'
},
#I can't return raw bytes so I transform it into a string
'body' : output.getvalue().hex(),
}
Am I sending the file the wrong way?
I tried sending it as base64 but it still was corrupted
It works if I make a test locally using python and just bytes.fromhex() the api response and write it in a file in byte mode.
I wonder if AWS Api Gateway is automatically encoding my body as base64