我在这里关注了这个令人敬畏的资源 - https://forums.aws.amazon.com/thread.jspa?messageID=673012 - 但是如果数据有空字段(我的意图,而且超出我的控制范围),它会抛出内部服务器错误。
然后搜索并实施了Christian E Willman最近更新的库 - https://github.com/christianewillman/aws-api-gateway-bodyparser
尽管更简洁,但当我发送空字段时(通过Postman),它仍然失败。任何有关使其正常工作的提示将不胜感激!
这里是参考的映射模板:
## 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
}
编辑:这是我发送的数据:
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
预期输出是一系列JSON键值对:
{
"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"
}
答案 0 :(得分:1)
结束从@ marcus-whybrow找到这个映射模板解决了问题 - 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
}