0

I followed this awesome resource here -- https://forums.aws.amazon.com/thread.jspa?messageID=673012 -- but it throws an internal server error if the data has empty fields (which mine will, and its beyond my control).

Then went searching and implemented this more recently updated library from Christian E Willman - https://github.com/christianewillman/aws-api-gateway-bodyparser

Despite being more terse, it's still failing when I send empty fields (via Postman). Any tips on getting it working would be greatly appreciated!

Here's the mapping template for reference:

## Parses x-www-urlencoded data to JSON for AWS' API Gateway
##
## Author: Christian E Willman <christian@willman.io>

#if ( $context.httpMethod == "POST" )
  #set( $requestBody = $input.path('$') )
#else
  #set( $requestBody = "" )
#end

#set( $keyValuePairs = $requestBody.split("&") )
#set( $params = [] )

## Filter empty key-value pairs
#foreach( $kvp in $keyValuePairs )
  #set( $operands = $kvp.split("=") )

  #if( $operands.size() == 1 || $operands.size() == 2 )
    #set( $success = $params.add($operands) )
  #end
#end

{
  ## add in the API stage data
  "stage": "$context.stage",
  #foreach( $param in $params )
    #set( $key = $util.urlDecode($param[0]) )

    #if( $param.size() > 1 )
      #set( $value = $util.urlDecode($param[1]) )
    #else
      #set( $value = "" )
    #end

    "$key": "$value"#if( $foreach.hasNext ),#end
  #end
}

Edit: This is the data I'm sending in:

WSO_SIGNATURE=1d9ce78c2778ecf795a93009a09f8102dd4ef38a&WP_BUYER_NAME=Neil+calabroso&WP_ITEM_NAME=Welcome+to+the+Vault&SHIPTONAME=Neil+calabroso&WSO_PRODUCT_ID=wp_product_3&WP_AFFID=&WSO_SALE_CURRENCY=USD&EMAIL=nn%2Bpaypal%40freelancer.com&WP_ITEM_NUMBER=wp_product_3&PAYMENTSTATUS=COMPLETED&WP_BUYER_EMAIL=nn%2Bpaypal%40freelancer.com&WSO_PRODUCT_NAME=Welcome+to+the+Vault&WP_SALEID=wp_sale_3&AMT=43&WSO_CUSTOMER_EMAIL=nn%2Bpaypal%40freelancer.com&WSO_SALE_ACTION=SALE&WP_SALE_AMOUNT=43&WSO_TXN_ID=35&saleid=wp_sale_3&WSO_AFF_AMOUNT=0.00&WP_ACTION=sale&WSO_IPN_VERSION=1&WP_TXNID=35&WSO_SALE_ID=wp_sale_3&payer_email=nn%2Bpaypal%40freelancer.com&WP_SALE_CURRENCY=USD&WSO_SALE_AMOUNT=43&WSO_AFF_EMAIL=&TRANSACTIONID=35&WSO_CUSTOMER_NAME=Neil+calabroso

The expected output is a series of JSON key-value pairs:

{
    "WSO_SIGNATURE": "1d9ce78c2778ecf795a93009a09f8102dd4ef38a",
    "WP_BUYER_NAME": "Neil calabroso",
    "WP_ITEM_NAME": "Welcome to the Vault",
    "SHIPTONAME": "Neil calabroso",
    "WSO_PRODUCT_ID": "wp_product_3",
    "WP_AFFID": "",
    "WSO_SALE_CURRENCY": "USD",
    "EMAIL": "nn+paypal@freelancer.com",
    "WP_ITEM_NUMBER": "wp_product_3",
    "PAYMENTSTATUS": "COMPLETED",
    "WP_BUYER_EMAIL": "nn+paypal@freelancer.com",
    "WSO_PRODUCT_NAME": "Welcome to the Vault",
    "WP_SALEID": "wp_sale_3",
    "AMT": "43",
    "WSO_CUSTOMER_EMAIL": "nn+paypal@freelancer.com",
    "WSO_SALE_ACTION": "SALE",
    "WP_SALE_AMOUNT": "43",
    "WSO_TXN_ID": "35",
    "saleid": "wp_sale_3",
    "WSO_AFF_AMOUNT": "0.00",
    "WP_ACTION": "sale",
    "WSO_IPN_VERSION": "1",
    "WP_TXNID": "35",
    "WSO_SALE_ID": "wp_sale_3",
    "payer_email": "nn+paypal@freelancer.com",
    "WP_SALE_CURRENCY": "USD",
    "WSO_SALE_AMOUNT": "43",
    "WSO_AFF_EMAIL": "",
    "TRANSACTIONID": "35",
    "WSO_CUSTOMER_NAME": "Neil calabroso"
}
Bryce York
  • 956
  • 1
  • 15
  • 35

1 Answers1

2

Ended up finding this mapping template from @marcus-whybrow solved the problem -- How to pass a params from POST to AWS Lambda from Amazon API Gateway

{
    #foreach( $token in $input.path('$').split('&') )
        #set( $keyVal = $token.split('=') )
        #set( $keyValSize = $keyVal.size() )
        #if( $keyValSize >= 1 )
            #set( $key = $util.urlDecode($keyVal[0]) )
                #if( $keyValSize >= 2 )
                    #set( $val = $util.urlDecode($keyVal[1]) )
                #else
                    #set( $val = '' )
                #end
            "$key": "$val"#if($foreach.hasNext),#end
        #end
    #end
}
Community
  • 1
  • 1
Bryce York
  • 956
  • 1
  • 15
  • 35