Get Raw Body from request

Hey im trying to implement stripe webhooks with lambda functions and i can’t pass stripe signature validation : https://stripe.com/docs/webhooks/signatures
it seems the body is being modified somewhere and the validation fails

if i add a custom request template to get the raw body , serverless remove all other variables and i can’t handle the event with express

functions:
  create:
    handler: posts.create
    events:
      - http:
          method: get
          path: whatever
          integration: lambda
          request:
            template:
              application/json: '{ "rawbody": "$util.escapeJavaScript($input.body)" }'

is there a way to “extend” a template instead of override it ? would like to add the raw body like this :
“rawbody”: “$util.escapeJavaScript($input.body)”,

thanks

1 Like

You might find it easier to implement using Lambda Proxy integration.

I ran into the same issue while trying to implement stripe webhooks

To validate and process a stripe webhook, the raw body of the request and the stripe signature from the request header is required.The config in the snippet above will only provide the raw body.

The snippet below is enhances the mapping template to provide both raw body and request headers required to process stripe webhook events.

stripeWebhook:
  handler: src/functions/Controller.stripeWebhook
  memorySize: 256
  events:
    - http:
        method: post
        cors: true
        path: /v1/Stripe/Webhook
        integration: lambda
        request:
          template:
            application/json: '{ "rawbody": "$util.escapeJavaScript($input.body)",
            "headers": {
              #foreach($header in $input.params().header.keySet())
              "$header": "$util.escapeJavaScript($input.params().header.get($header))" #if($foreach.hasNext),#end
              #end
            } }'

Hope this helps someone :slight_smile: