I got an issue where I have a requirement for a lambda to process an image and return the actual image (not the link).
I pin pointed the cause to API Gateway because if the request adds the header Accept: image/*
the API works as intended. Unfortunately clients can vary, one specific client is mobile and they cannot set the Accept header as it's set by the framework and states that they cannot change it.
The lambda itself returns an encoded base64 image. Here's an example response:
{
"statusCode": 200,
"headers": {
"Cache-Control": "public, max-age=31536000",
"Content-Type": "image/jpeg",
"X-Content-Transfer-Encoding": "binary",
"X-Content-Type": "image/jpeg",
"X-Encoded-Size": "10720",
"X-Image-Size": "8040"
},
"multiValueHeaders": null,
"body": "/9j/2wCEAAUDBAQEAwUEBAQFBQUGBwwIBwcH.........", // <-- trimmed
"isBase64Encoded": true
}
The image is encoded correctly. API Gateway settings for Binary media types is set for image/*
. I tried all combinations on this from */*
to all types of images.
For context, this is what I return from the API (Golang):
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Body: base64.StdEncoding.EncodeToString(img.Content),
Headers: map[string]string{
"Content-Type": img.ContentType,
"Cache-Control": "public, max-age=31536000",
"X-Content-Transfer-Encoding": "binary",
// diagnostics headers
"X-Image-Size": fmt.Sprintf("%d", len(img.Content)),
"X-Encoded-Size": fmt.Sprintf("%d", len(base64.StdEncoding.EncodeToString(img.Content))),
"X-Content-Type": img.ContentType,
},
IsBase64Encoded: true,
}
Did I miss something?
Try to modify your API Gateway's config:
Add the mapping:
{
"contentHandling": "CONVERT_TO_BINARY",
"statusCode": 200
}
When the Lambda comes back with the base64 Image, the API Gateway will force conversion to binary regardless of whether the image/*
is there or not.
Check out this for more details: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-integration-settings-integration-response.html