I’m creating a microservice to dynamically generate EXCEL files and download them.
I have an AWS Api Gateway and a Python 3.8 Lambda function. To create the EXCEL I use the Openpyxl package.
The file is generated fine but when I download it I seem to get some encoding problem I can’t figure out.
The Lambda
Here I summarize the function, highlighting the end, where I save the file to a buffer and return it.
wb = I create my Workbook correctly
buffer = io.BytesIO()
wb.save(buffer)
excel_final = buffer.getvalue()
buffer.close()
response = excel_final
return response
When testing the Lambda output, I see this response, that look Unicode:
“PK\u0003\u0004\u0014\u0000\u0000\u0000\b\u0000}t\u008dT\u………….”
Api Gateway
The Integration Response is set to Passthrough and the Method Response to application/xml. Tried with different setups but couldn’t get it working.
When I test the API method, I also get the data in the following format, I think it’s utf-8 decoded:
“PK�u�TAMb��docProps/app.xmlM�= 1D��q��A�Bb@�R��{/�dC�B~�9��noF� g*�-�T��"���N]�n�h�cy;�Ό�HI`��� ���M��F�r�xN��pe'å! �rmީ�5�&����;i^PK�u�T�z���docProps/core.xml���N�0�_e�u�V=DY.C�@Bb�[�x[E�F�Q��'-[������V����
Javascript
In my request.onload I get the bytes response and turn it into a Blob for download.
let blob = this.response;
let final_blob = new Blob([blob], {type: 'application/xml'})
The file is downloaded but recognized as corrupt. Can’t open it.
Tests
I tried different Content-Types, coding/encoding in Lambda or Javascript, but never got it working.
Any clues welcome!