I’m attempting to set a function that returns a binary file.
I’m using the serverless-apigwy-binary plugin (and also, serverless-apigw-binary, but i don’t think i need it).
In the serverless yml i have:
functions:
getImage:
handler: handler.getImage
events:
- http:
integration: lambda
path: getImage
method: get
contentHandling: CONVERT_TO_BINARY
And in the handler i have
def getImage(event, context):
img_jpg = open(‘./test.jpg’, ‘rb’).read()
out_b64 = b64encode(img_jpg)
out_str = out_b64.decode(‘utf8’)
return {
‘statusCode’: 200,
‘body’: out_str,
‘isBase64Encoded’: True,
‘headers’: {‘Content-Type’: ‘image/jpeg’},
}
however, on postman i get “Internal server error” and when testing in “API Gatway” i get:
Tue Jun 05 15:17:45 UTC 2018 : Execution failed due to configuration error: Unable to base64 decode the body.
Tue Jun 05 15:17:45 UTC 2018 : Method completed with status: 500
Any suggestions?
amitm02:
serverless-apigwy-binary
Okay, it turns out the i needed to take out “integration: lambda”. (unlike stated in https://github.com/ryanmurakami/serverless-apigwy-binary
For some reason it stopped working again.
Keep getting: Execution failed due to configuration error: Unable to base64 decode the body.
After spending a day trying to fix it with no success, i’ve decided to use S3 for the file download.
buggy
June 6, 2018, 10:06am
4
If you still want help can you post your full handler?
My handler is posted in the original post. It is pretty much it except some imports.
buggy
June 6, 2018, 10:35am
6
When debugging code everything is important. “pretty much” can’t be debugged. Things I don’t know but suspect:
getImage()
is being export somewhere.
Your runtime is Node 8.1 but you didn’t declare the handler async
(or maybe you just left it out along with the export
)
The read()
in open(’./test.jpg’, ‘rb’).read()
is actually returning a Promise while b64encode()
requires something else.
out_b64.decode()
isn’t returning a string.
Most of your comments referring to nodejs.
The code is in python, not not js.
The error is not in the lambdas. (no error logs there). it is from API Gatway “test” button in aws console.
buggy
June 6, 2018, 11:18am
8
Then I would focus on
out_b64 = b64encode(img_jpg)
out_str = out_b64.decode(‘utf8’)
and
‘body’: out_str,
It seems like you’re base64 encoding the file, then decoding it and sending the decoded file as the body
Can you try this:
def getImage(event, context):
img_jpg = open(’./test.jpg’, ‘rb’).read()
out_b64 = b64encode(img_jpg)
return {
‘statusCode’: 200,
‘body’: out_b64,
‘isBase64Encoded’: True,
‘headers’: {‘Content-Type’: ‘image/jpeg’},
}