2

I have just read lots of forum messages and some stackoverflow messages. I tried everything. However, I could not send a PNG response through API Gateway. I use LAMBDA_PROXY in API Gateway.

In Lambda, I have a code like this:

exports.handler = (event, context, callback) => {

// some code here ...

image.encode("png",function(err, buffer) {

    var generated = buffer.toString('base64');

    var response = {
        statusCode: 200,
        headers: 
        {
            "Content-Type": "image/png"
        },
        body: generated,
        isBase64Encoded: true
    };

    callback(null, response);
});
}

Binary Media Types:

{
"apiKeySource": "HEADER", 
"name": "testimage", 
"createdDate": 1518592036, 
"binaryMediaTypes": [
    "image/png"
], 
"endpointConfiguration": {
    "types": [
        "EDGE"
    ]
}, 
"id": "xxx"
}

Content Handling Type:

{
"statusCode": "200", 
"contentHandling": "CONVERT_TO_BINARY", 
"responseTemplates": {
    "application/json": null
}
}

When I open the API Gateway URL on the browser I see image/png as content-type. but the response is encoded. so, browser can't interpret it. I see black screen.

I sent a CURL request like this: {code}curl -H "Accept: image/png" https://xxx.execute-api.eu-central-1.amazonaws.com/testimage/myimage > /opt/1.png{code}

This one doesn't work, either.

What is wrong? Can you help me?

  • 1
    Set `Binary Media Types` to `*/*`. – Michael - sqlbot Feb 21 '18 at 00:47
  • @Michael-sqlbot It worked! Thank you so much! I really appreciate your help! – user9387220 Feb 21 '18 at 06:59
  • Possible duplicate of [Amazon Lambda Java function returning png to API gateway](https://stackoverflow.com/questions/37283932/amazon-lambda-java-function-returning-png-to-api-gateway) – Alex R Mar 29 '19 at 07:12
  • useful link for those new to serverless: https://www.serverless.com/blog/framework-release-v142 which shows where and how to add binaryMediaTypes – Mozart Mar 09 '21 at 14:48
  • If possible it is better to use S3 for handling your file upload processes for many reasons: its cheaper, its faster, it has no filesize limit (6MB on API Gateway) and requires no hacky settings for binary media types for a service not made to handle binary data. – Gareth McCumskey Mar 09 '21 at 15:08

0 Answers0