Reputation: 285
I have a static website hosted on AWS Cloudfront. On a route I need to accept the POST method because is the redirect of the OAuth server so I decided to develop a Lambda@edge.
My idea is to register the lambda on the 'Viewer Request' and intercept the POST method, reading the body and copy the values on the headers in order to make them readable from my static website (I know I can access the Referrer header with javascript).
I set up the Lambda and I can intercept the POST letting all the other methods pass through.
The problem is that I cannot find a way to read the body of the POST request, I googled it with no result.
Any advice on I can do it? Is there any parameter I have to configure on Cloudfront side?
Upvotes: 3
Views: 3697
Reputation: 1936
The accepted answer was correct, but AWS has introduced the feature to allow access Request Body.
To access it from request:
const body = Buffer.from(request.body.data, 'base64').toString();
To configure it in cloudfront:
Upvotes: 12
Reputation: 179134
The body of viewer requests and origin responses is not available to Lambda@Edge functions -- only the headers.
While it isn't entirely clear what you are trying to do once you get access to the data in the body, if that's something you need, then here's the AWS workaround:
Look into API Gateway, which does have access to the request body. You can deploy an API Gateway Regional Endpoint and add that endpoint as a second origin to your CloudFront distribution. You can then use Lambda@Edge in an Origin Request trigger to divert those requests to the alternate origin (your new "API," which can generate the response you want, based on the POST
request you receive).
Upvotes: 5