Skip to content

Commit 6239f57

Browse files
committed
Set isBase64Encoded based on HTTP response body type
This commit changes the way `isBase64Encoded` attribute is set for the Lambda HTTP response. Currently, it is hardcoded to `false` which prevents an API endpoint from returning binary data to the caller. The problem is that Lambda uses this attribute to signal API Gateway that the response is base64 encoded which is then used by API Gateway to possibly convert the response payload to binary before sending it back to the caller. I think the change is safe because it doesn't actuate any change in isolation: an API Gateway must first be configured to convert a base64 encoded payload into binary. This setting is the `Binary media types` which indicates which `content-type` should be treated as binary (both from client and server side). More information can be found at https://docs.aws.amazon.com/apigateway/latest/developerguide/lambda-proxy-binary-media.html.
1 parent 22d998d commit 6239f57

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

‎openapi-lambda/src/lib.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ pub type HttpResponse = Response<Body>;
3434
/// Serialize an [`HttpResponse`] as an [`ApiGatewayProxyResponse`].
3535
pub fn http_response_to_apigw(response: HttpResponse) -> ApiGatewayProxyResponse {
3636
let (parts, body) = response.into_parts();
37+
let is_base64_encoded = match body {
38+
Body::Binary(_) => true,
39+
_ => false,
40+
};
3741
ApiGatewayProxyResponse {
3842
status_code: parts.status.as_u16() as i64,
3943
headers: Default::default(),
4044
multi_value_headers: parts.headers,
4145
body: Some(body),
42-
is_base64_encoded: false,
46+
is_base64_encoded: is_base64_encoded,
4347
}
4448
}
4549

0 commit comments

Comments
 (0)