3
votes

I want to return a pdf from a AWS Lambda function, and I use API Gateway for calling it from a any browser.

I have a aws lambda function in c# that returns an API Gateway Response which body is a pdf in base64 encoded string.

So far the endpoint returns a file with .pdf extension but not in binary. Instead, is a text file with the base64 string.

The headers i'm returning from the c# code are:

  var headersDic = new Dictionary<string, string>();
      headersDic.Add("Content-type", "application/pdf");
      headersDic.Add("Content-disposition", "attachment; filename=file.pdf");

I converted manually the base64 string to binary file and opened it as a pdf, and it works, I mean, the base64 string is correct so I assume the problem is being the API Gateway.

In the integration response console of the API Gateway, I got this:

enter image description here

But i'm not able to make it work.

I also have binary media types enabled.

enter image description here

3
Hi do u have any solution for this? - wapt49

3 Answers

0
votes

I don't know exactly what I did, but I deleted the 'ALL' method, and created the 'GET' method with this config and now it works.

enter image description here

0
votes

You also need to declare the content type like the following in your LambdaEntryPoint.cs

See this documentation: https://github.com/aws/aws-lambda-dotnet/blob/master/Libraries/src/Amazon.Lambda.AspNetCoreServer/README.md

You have to tell Lambda you're going to return something besides strings.

public class LambdaEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
{

    protected override void Init(IWebHostBuilder builder)
    {
        builder
            .UseStartup<Startup>();

        ****IMPORTANT PART HERE****
        RegisterResponseContentEncodingForContentType("application/pdf",
            ResponseContentEncoding.Base64);
    }
}
0
votes

Response Headers

'Content-Type': 'application/pdf'
'Content-disposition', 'attachment; filename=fileName.pdf'

Response to Client

{
 statusCode: 200
 headers: responseHeader,
 body: pdfContent.toString(base64),
 isBase64Encoded: true,
}

API Gateway Binary Media Types

enter image description here

Hope it will help