在 `AWS::Serverless::Api` 上启用 `BinaryMediaTypes` 会对请求负载体进行 Base64 编码

作者: davidjb创建于 2025年7月22日更新于 2026年7月31日
标签stage/needs-triage

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 suggest 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

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

Observed result

内容来源: aws/serverless-application-model