4

I'm not sure whether this is a Micronaut or a AWS Gateway question. Any help would be fantastic.

I am trying to use the Micronaut framework to create a Lambda function that returns a pdf through the AWS API Gateway. Is this supported? What do I need to change to return binary content? I have tried changing the return type of the method to byte[] but it looks like the Content-Type is always application/json.

As a bit of context, I am hoping to use Groovy for writing the function, and Dynamic Reports to create the PDF using data from DynamoDB.

Many thanks in advance.

2
  • 1
    Be wary returning PDF's via the API Gateway, there is a limit to the bytes you can return, I think it's 10Mb. Also it uses compute time to deliver the PDF, so it's a good idea to check if it's over a certain size, then instead of returning the PDF, save the PDF to an S3 bucket, create an expiring signed URL to the file and return the URL instead. You can then apply a lifecycle against the bucket to delete old files. Commented May 31, 2018 at 13:00
  • Great - thanks for the tip :-) Commented Jun 1, 2018 at 14:09

2 Answers 2

2

I have now managed to get this working.

I have created a response object:

class ReportResponse {

    boolean isBase64Encoded = true
    def headers
    byte[] body
}

and then in my handler I have:

ReportResponse reports(data, Context context) {
    return new ReportResponse(
        headers:  [ "Content-Type": "application/pdf" ],
        body: JasperExportManager.exportReportToPdf(new 
    MemberReport().getReport(data.queryStringParameters.id)))
}

Unfortunately, when I hardcoded the headers in the ReportResponse class, there was a console error (something about OpenJDK..)

The above allows me to use Lambda Proxying in the AWS API Gateway and so the full request object is passed through to the function.

The only other change is to set '*/*' as binary in the AWS API Gateway which is fine for my use case.

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried with @Produces?

1 Comment

Hi - yep tried that. Thanks for the idea though :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.