Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS API Gateway Binary output without Accept header

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

like image 230
pakore Avatar asked Dec 08 '16 10:12

pakore


People also ask

How do I pass headers in AWS API gateway?

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.

How do I add binary media type API gateway?

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.

Does API gateway pass authorization header to Lambda?

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.


1 Answers

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.

like image 95
Kenneth Rory Dunn Avatar answered Sep 18 '22 00:09

Kenneth Rory Dunn