AWS网关+ x-www-form-urlencoded以空字段中断



我在这里遵循了这个很棒的资源——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"
}

最终从@marcus找到了这个映射模板-whybrow解决了问题-如何从Amazon API Gateway将参数从POST传递到AWS Lambda

{
    #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
}

相关内容

  • 没有找到相关文章

最新更新