AWS API Gateway base64Decode produces garbled binary?

前端 未结 3 1755
眼角桃花
眼角桃花 2020-11-28 07:59

I\'m trying to return a 1px gif from an AWS API Gateway method.

Since binary data is now supported, I return an image/gif using the following \'Integration Response\

3条回答
  •  天命终不由人
    2020-11-28 08:42

    It looks like this was a known issue previously: https://forums.aws.amazon.com/thread.jspa?messageID=668306򣊒

    But it should be possible now that they've added support for binary data: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html

    It looks like this is the bit we need: "Set the contentHandling property of the IntegrationResponse resource to CONVERT_TO_BINARY to have the response payload converted from a Base64-encoded string to its binary blob". Then we shouldn't need the base64Decode() function.

    Working on a test now to see if this works.

    EDIT: I was finally able to get this working. You can see the binary image here: https://chtskiuz10.execute-api.us-east-1.amazonaws.com/prod/rest/image

    Here's my Lambda function that returns the base64 encoded PNG as a string: https://gist.github.com/davemaple/73ce3c2c69d5310331395a0210069263

    I updated the method response as follows:

    I updated the integration response to include a hard-coded image/png header:

    The last step was tricky: setting the contentHandling property to "CONVERT_TO_BINARY". I couldn't figure out how to do in the AWS console. I had to use the CLI API to accomplish this:

    aws apigateway update-integration-response \
        --profile davemaple \
        --rest-api-id chtskiuzxx \
        --resource-id ki1lxx \
        --http-method GET \
        --status-code 200 \
        --patch-operations '[{"op" : "replace", "path" : "/contentHandling", "value" : "CONVERT_TO_BINARY"}]'
    

    I hope this helps.

提交回复
热议问题