I'm using API Gateway Lambda proxy integration and trying to return a binary application/protobuf
response. No matter what I do, the response body is always a base64 encoded string
application/protobuf
setup as a binary media types
in APIGPOST
:
Accept: application/protobuf
Content-Type: application/protobuf
content-type: application/protobuf
, and correctly setting the IsBase64Encoded
Lambda response to true
How do you get APIG to base64 decode the string? I swear I had this working a few months ago when I 1st tried this.
Note: I've also tried */*
as a binary media types
Some related posts to add background:
Update:
Turns out I can only get it working if binary media type
is set to */*
. The client Accept
header has no impact once it is set to this.
Many bad side effects of using */*
because every response is attempted to get decoded (even when IsBase64Encoded is false or not set)
I thought it wasn't decoding because Chrome network inspect tools will always show binary data as base64 encoded in the Preview
tab. You can see the protobuf in the Response
tab.
Lambda proxy integration is a lightweight, flexible API Gateway API integration type that allows you to integrate an API method – or an entire API – with a Lambda function. The Lambda function can be written in any language that Lambda supports.
To enable CORS for the Lambda proxy integration, you must add Access-Control-Allow-Origin: domain-name to the output headers . domain-name can be * for any domain name. The output body is marshalled to the frontend as the method response payload.
The easiest way to set custom HTTP status code is to setup a Lambda Proxy Integration in API Gateway. In API Gateway > Resource > Actions Dropdown > Create Method > tick Lambda Proxy Integration and select appropriate Lambda function. For async functions just return with an object with statusCode and body .
The problem was I'm using CloudFront in front of API Gateway, and I was not passing the Accept
header to the origin (APIG).
The docs on handling binary with Lambda proxy are not great, so here is a quick summary:
Accept
header who's 1st media type matches what you have set as a binary media types
in API GatewayIsBase64Encoded
to true AND the body
must be base64 encodedIf the clients Accept
header matches an entry in API Gateway's binary media types
and these conditions are met, API Gateway will transform (base64 decode) before sending a response to the client.
This blog post walks you through step-by-step on how to get it working (without CloudFront).
This is a full blown aws-blueprint for getting a production grade ci/cd with CloudFront.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With