How to response binary content with AWS Lambda Container and ASP.NET Minimal API? #1527
-
First I return app.MapGet("/proof", async Task<APIGatewayHttpApiV2ProxyResponse> () =>
{
var rawBytes = await GenerateImageAsync(doc, ImageFormat.Webp);
var response = new APIGatewayHttpApiV2ProxyResponse() {
StatusCode = 200,
Body = Convert.ToBase64String(rawBytes),
IsBase64Encoded = true
};
response.SetHeaderValues("Content-Type", "image/webp", false);
return response;
}); command used in local testing curl http://localhost:8080/2015-03-31/functions/function/invocations -d @event.json event.json{
"version": "2.0",
"routeKey": "$default",
"rawPath": "/proof",
"rawQueryString": "",
"cookies": [],
"headers": {
"Content-Type": "text/plain"
},
"queryStringParameters": {},
"requestContext": {
"accountId": "123456789012",
"apiId": "api-id",
"authentication": null,
"authorizer": null,
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"http": {
"method": "GET",
"path": "/proof",
"protocol": "HTTP/1.1",
"sourceIp": "192.0.2.1",
"userAgent": "agent"
},
"requestId": "id",
"routeKey": "$default",
"stage": "$default",
"time": "12/Mar/2020:19:03:58 +0000",
"timeEpoch": 1583348638390
},
"body": "",
"pathParameters": {},
"isBase64Encoded": false,
"stageVariables": {}
} received response{
"statusCode": 200,
"headers": {
"Content-Type": "application/json; charset=utf-8"
},
"body": "{\"statusCode\":200,\"headers\":{\"Content-Type\":\"image/webp\"},\"cookies\":null,\"body\":\"UklGRurSA......AAAAA\",\"isBase64Encoded\":true}",
"isBase64Encoded": false
} expected response{
"statusCode": 200,
"headers": {
"Content-Type": "image/webp"
},
"cookies": null,
"body": "UklGRurSA......AAAAA",
"isBase64Encoded": true
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@mikucat0309 Good afternoon. Just curious, have you enabled Binary Content in your API Gateway REST API settings? Kindly refer Enabling binary support using the API Gateway console. I tested it with ASP.NET minimal API using your code and got this content when hitting the {
"statusCode":200,
"headers": {
"Content-Type":"image/jpg"
},
"cookies":null,
"body":"/9j/2wCEAA.........2BAMB/1p0qzHhv2P/9k=",
"isBase64Encoded":true
} Thanks, |
Beta Was this translation helpful? Give feedback.
-
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
@mikucat0309 I'm unsure what you might be doing wrong, but I changed logic to add
builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi);
in my project and integrated it with HTTP API, it returns the following response: