Skip to content

Enabling BinaryMediaTypes on AWS::Serverless::Api Base64-encodes request payload body #3794

Open

Description

@davidjb

Description

When enabling AWS::ApiGateway::RestApi's BinaryMediaTypes for a particular type (such as application/json), this causes the request's body to have been Base64-encoded when received, as well as the expected behaviour of allowing the API to return binary data as a Base64-encoded body within the response payload.

This is unexpected behaviour given the documentation (see below for why), or it could be a bug. Getting clarification on which it is is critical to the use of these resources in AWS; if a documentation issue, the published info should be clearly explained.

The docs on AWS::ApiGateway::RestApi's BinaryMediaTypes suggests this only affects the response (emphasis mine):

List of MIME types that your API could return. Use this to enable binary support for APIs. Use ~1 instead of / in the mime types.

AWS CloudFormation compatibility: This property is similar to the BinaryMediaTypes property of an AWS::ApiGateway::RestApi resource. The list of BinaryMediaTypes is added to both the AWS CloudFormation resource and the OpenAPI document.

The docs above appear outdated as the Use ~1 instead of / in the mime types. reference no long applies - using application/json works fine via SAM locally and when deployed to production AWS. Online resources such as this StackOverflow answer suggest confusion and suggest the AWS docs were wrong and that the AWS::ApiGateway::RestApi has since been fixed up.

On the linked AWS::ApiGateway::RestApi, the docs are unclear:

BinaryMediaTypes: The list of binary media types supported by the RestApi. By default, the RestApi supports only UTF-8-encoded text payloads.

Steps to reproduce

Resources:
  MyAPI:
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod
      BinaryMediaTypes: ["application/json"]

Observed result

  1. Deploy a SAM template such as that above, with a corresponding Lambda function
  2. Observe that in an AWS Lambda function, the isBase64Encoded property on the request is true and the body is encoded with Base64.

Expected result

  1. Establish if the behaviour of Base64-encoding the request payload body when BinaryMediaTypes is enabled is expected.

  2. Update the documentation for https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html#sam-api-binarymediatypes accordingly:

    1. Indicate that the BinaryMediaTypes option will Base64-encode the request body and require that the response body be Base64-encoded.
    2. Remove or correct the Use ~1 instead of / in the mime types. reference.
  3. Update the document for https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-restapi.html#cfn-apigateway-restapi-binarymediatypes accordingly to indicate the same impact above.

If the behaviour is not correct, then the resolution would be to fix how SAM and AWS operate.

Additional environment details

  1. OS: Ubuntu 24.04
  2. If using the SAM CLI, sam --version: SAM CLI, version 1.142.1
  3. AWS region: us-east-1

Activity

  1. added
    stage/needs-triage
    on Jul 22, 2025
  2. roger-zhangg commented on Jul 2, 2026

    @roger-zhangg
    Member

    Hi, thanks for reporting this issue. We're triaging and would like to understand more to proceed:

    1. Is this behavior observed during local development (sam local start-api) or on the deployed API Gateway, or both?
    2. Could you provide a minimal SAM template that reproduces the issue?
    3. What content types are you sending in the request body when you observe the base64 encoding?

    This will help us narrow down whether the fix belongs in the SAM transform, the local API emulation, or if this is expected API Gateway behavior that needs documentation. Thanks!

  3. davidjb commented on Jul 2, 2026

    @davidjb
    Author

    Thanks for the reply @roger-zhangg. To answer your questions:

    1. Is this behavior observed during local development (sam local start-api) or on the deployed API Gateway, or both?

    Both local development with SAM and with a deployed API gateway to AWS proper.

    1. Could you provide a minimal SAM template that reproduces the issue?

    Effectively the segment above in the PR description is enough to trigger the issue for me -- the addition of the line:

           BinaryMediaTypes: ["application/json"]

    I can put together a complete SAM template with gateway + Lambda + example output etc, but that'll take a little time with my other commitments.

    1. What content types are you sending in the request body when you observe the base64 encoding?

    I've tested with application/json (as the example shows), and in my Go-based Lambdas, am seeing that request.IsBase64Encoded is true when received. I've not tested with other content types as my APIs are JSON-based, but can whip something together if that would help.

    For what it's worth, I've taken to writing all my Lambdas with conditional support for request.IsBase64Encoded, since otherwise the Lambda has to know the configuration of the API gateway to operate correctly.

  4. roger-zhangg commented on Jul 31, 2026

    @roger-zhangg
    Member

    Thanks for the thorough answers @davidjb, and for confirming it reproduces both locally and against deployed API Gateway. That last detail is the key one, and it points at where the real problem is.

    This is API Gateway behaviour, not something SAM introduces

    BinaryMediaTypes is a pass-through in the transform — the only processing SAM does is replacing ~1 with / before writing it to x-amazon-apigateway-binary-media-types in the OpenAPI document (samtranslator/swagger/swagger.py:add_binary_media_types), plus setting the same list on the AWS::ApiGateway::RestApi resource. There's no request/response asymmetry SAM could introduce, which matches your observation that deployed API Gateway behaves the same way.

    The underlying behaviour is by design in API Gateway: binaryMediaTypes governs how the payload is handled in both directions. When an incoming Content-Type matches an entry in the list and the integration is Lambda proxy, API Gateway base64-encodes the request body and sets isBase64Encoded: true. So listing application/json opts your request bodies into that treatment as well as your responses.

    So the actionable part here is the documentation, and I agree it's misleading. The current text is:

    List of MIME types that your API could return. Use this to enable binary support for APIs.

    "could return" reads as response-only, which is what led you here. It should say the list affects request payload handling too, and that isBase64Encoded will be set on requests whose Content-Type matches.

    You were also right that the Use ~1 instead of / in the mime types sentence has since been dropped from the current published page, so it's had a partial update already.

    I'm routing this to the docs owners for the wording fix rather than treating it as a transform bug. No need to spend time assembling the full template + Lambda repro — your snippet plus the confirmation that it happens deployed is sufficient to establish this, so please don't put more of your time into it.

    For anyone hitting this: your approach of writing handlers to check isBase64Encoded unconditionally is the right one. The flag can be set for reasons outside your template's control, so treating it as authoritative rather than inferring it from configuration is the robust pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stage/needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions