Menu
Amazon API Gateway
Developer Guide

Enable Binary Support Using API Gateway REST API

The following tasks show how to enable binary support using the API Gateway REST API calls.

Add and Update Supported Binary Media Types to an API

To enable API Gateway to support a new binary media type, you must add the binary media type to the binaryMediaTypes list of the RestApi resource. For example, to have API Gateway handle JPEG images, submit a PATCH request to the RestApi resource:

Copy
PATCH /restapis/<restapi_id> { "patchOperations" : [ { "op" : "add", "path" : "/binaryMediaTypes/image~1jpeg" } ] }

The MIME type specification of image/jpeg that is part of the path property value is escaped as image~1jpeg.

To update the supported binary media types, replace or remove the media type from the binaryMediaTypes list of the RestApi resource. For example, to change binary support from JPEG files to raw bytes, submit a PATCH request to the RestApi resource, as follows.

Copy
PATCH /restapis/<restapi_id> { "patchOperations" : [{ "op" : "replace", "path" : "/binaryMediaTypes/image~1jpeg", "value" : "application/octet-stream" }, { "op" : "remove", "path" : "/binaryMediaTypes/image~1jpeg" }] }

Configure Request Payload Conversions

If the endpoint requires a binary input, set the contentHandling property of the Integration resource to CONVERT_TO_BINARY. To do so, submit a PATCH request, as shown next:

Copy
PATCH /restapis/<restapi_id>/resources/<resource_id>/methods/<http_method>/integration { "patchOperations" : [ { "op" : "replace", "path" : "/contentHandling", "value" : "CONVERT_TO_BINARY" }] }

Configure Response Payload Conversions

If the client accepts the result as a binary blob instead of a Base64-encoded payload returned from the endpoint, set the contentHandling property of the IntegrationResponse resource to CONVERT_TO_BINARY by submitting a PATCH request, as shown next:

Copy
PATCH /restapis/<restapi_id>/resources/<resource_id>/methods/<http_method>/integration/responses/<status_code> { "patchOperations" : [ { "op" : "replace", "path" : "/contentHandling", "value" : "CONVERT_TO_BINARY" }] }

Convert Binary Data to Text Data

To send binary data as a JSON property of the input to AWS Lambda or Kinesis through API Gateway, do the following:

  1. Enable the binary payload support of the API by adding the new binary media type of application/octet-stream to the API's binaryMediaTypes list.

    Copy
    PATCH /restapis/<restapi_id> { "patchOperations" : [ { "op" : "add", "path" : "/binaryMediaTypes/application~1octet-stream" } ] }
  2. Set CONVERT_TO_TEXT on the contentHandling property of the Integration resource and provide a mapping template to assign the Base64-encoded string of the binary data to a JSON property. In the following example, the JSON property is body and $input.body holds the Base64-encoded string.

    Copy
    PATCH /restapis/<restapi_id>/resources/<resource_id>/methods/<http_method>/integration { "patchOperations" : [ { "op" : "replace", "path" : "/contentHandling", "value" : "CONVERT_TO_TEXT" }, { "op" : "add", "path" : "/requestTemplates/application~1octet-stream", "value" : "{\"body\": \"$input.body\"}" } ] }

Convert Text Data to a Binary Payload

Suppose a Lambda function returns an image file as a Base64-encoded string. To pass this binary output to the client through API Gateway, do the following:

  1. Update the API's binaryMediaTypes list by adding the binary media type of application/octet-stream, if it is not already in the list.

    Copy
    PATCH /restapis/<restapi_id> { "patchOperations" : [ { "op" : "add", "path" : "/binaryMediaTypes/application~1octet-stream", }] }
  2. Set the contentHandling property on the Integration resource to CONVERT_TO_BINARY. Do not define a mapping template. When you do not define a mapping template, API Gateway invokes the passthrough template to return the Base64-decoded binary blob as the image file to the client.

    Copy
    PATCH /restapis/<restapi_id>/resources/<resource_id>/methods/<http_method>/integration/responses/<status_code> { "patchOperations" : [ { "op" : "replace", "path" : "/contentHandling", "value" : "CONTVERT_TO_BINARY" } ] }

Pass through a Binary Payload

To store an image in an Amazon S3 bucket using API Gateway, do the following:

  1. Update the API's binaryMediaTypes list by adding the binary media type of application/octet-stream, if it is not already in the list.

    Copy
    PATCH /restapis/<restapi_id> { "patchOperations" : [ { "op" : "add", "path" : "/binaryMediaTypes/application~1octet-stream" } ] }
  2. On the contentHandling property of the Integration resource, set CONVERT_TO_BINARY. Set WHEN_NO_MATCH as the passthroughBehavior property value without defining a mapping template. This enables API Gateway to invoke the passthrough template.

    Copy
    PATCH /restapis/<restapi_id>/resources/<resource_id>/methods/<http_method>/integration { "patchOperations" : [ { "op" : "replace", "path" : "/contentHandling", "value" : "CONVERT_TO_BINARY" }, { "op" : "replace", "path" : "/passthroughBehaviors", "value" : "WHEN_NO_MATCH" } ] }