5

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).

1
  • Dear Pakore You should accept Kenneth Rory Dunn answer -> it works Commented Sep 18, 2017 at 20:40

2 Answers 2

7

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:

  • Content handling: Convert to binary
  • Header Mappings: Content-Type: integration.response.header.Content-Type

This eliminates the need for an Accept request header from your client.

Sign up to request clarification or add additional context in comments.

2 Comments

Would you be able to provide an example for how to do this in CDK? I am struggling to get a PDF to show in the browser, because the browser does not send the Accept: application/pdf header. I already set binaryMediaTypes on the API resource, and also the CONVERT_TO_BINARY on the lambda integration. But I struggle to get the header mapping set up.
If you are trying to make this work in Lambda without a matching accept header, this will not work with a 'Lambda Proxy Integration'. As far as I can tell, you cant modify the content types directly from API gateway when using Lambda proxy. Use 'Lambda non-proxy (custom)' integration instead
0

If you want do this using the lambda proxy integration, you can pass a value of */* into the binary media types and then it won't try to match the accept header.

Here's an article that goes into a bit more detail about it https://www.sspilsbury.com/2018-03-04-getting-aws-lambda-and-api-gateway-to-support-binary-2018-edition/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.