According to http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html
If contentHandling is not defined, and if the Content-Type header of the response and the Accept header of the original request match an entry of the binaryMediaTypes list, API Gateway passes through the body. This occurs when the Content-Type header and the Accept header are the same; otherwise, API Gateway converts the response body to the type specified in the Accept header.
I can't make the original request to send the Accept
header.
How can I make my AWS API Gateway to return in binary format? (image/jpeg). Because the image I'm sending back from s3 gets converted to text in the last stage (because the original request lacks of the Accept: image/jpeg
header).
To pass custom headers from an API Gateway API to a Lambda function, use a body mapping template. The API sends the updated API request to a Lambda function to process the headers. Then, the Lambda function returns one or more header values from the original API request.
To enable binary support by using the API Gateway consoleUnder the selected API in the primary navigation panel, choose Settings. In the Settings pane, choose Add Binary Media Type in the Binary Media Types section. Type a required media type, for example, image/png , in the input text field.
For a Lambda authorizer of the REQUEST type, API Gateway passes request parameters to the authorizer Lambda function as part of the event object. The request parameters include headers, path parameters, query string parameters, stage variables, and some of request context variables.
Set the 'Content handling' in your integration response to 'Convert to binary'. When Convert to binary is set in the integration response, the Content-Type API Gateway response header is '*/*' so you need to create a header mapping in the integration response for Content-Type, mapped to integration.response.header.Content-Type. This will ensure that the API Gateway response includes the same Content-Type value the backend passed in it's response to API Gateway.
Just to be clear, here is a summary of the required settings.
Integration Response:
This eliminates the need for an Accept request header from your client.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With