【问题标题】:How I can receive parameter that when I send the message from postman当我从邮递员发送消息时如何接收参数
【发布时间】:2018-12-01 12:21:21
【问题描述】:

我通过邮递员将参数发送到api网关并接收结果。 所以我在 lambda 中尝试了一个简单的例子,

exports.handler = (event, context, callback) => {
    // TODO implement
    const Idx = event.Idx * 2;

    callback(null, Idx);

};

当我发送 Idx 时,它会回调它的两倍。

我在 Body 中选择 raw 并这样做我可以收到结果

但是,我把 "Content-Type" : "application/x-www-form-urlencoded" 放在 Headers 部分,选择 x-www-form-urlencoded,它会返回这个。

{"message": "Could not parse request body into json: Unrecognized token \'Idx\': was expecting \'null\', \'true\', \'false\' or NaN\n at [Source: [B@331a45ec; line: 1, column: 5]"}

我想通过 x-www-formurlencoded 从邮递员那里接收数据。

我该怎么做?

如果你知道,请帮助我。

【问题讨论】:

    标签: amazon-web-services aws-lambda postman


    【解决方案1】:

    API Gateway 需要 JSON,但您给它的是 urlencoded 表单数据(即字符串)。您需要在 API Gateway 配置中添加映​​射模板:

    {
        "data": {
            #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
        }
    }
    

    来源:https://stackoverflow.com/a/37956390/5794667

    【讨论】:

      猜你喜欢
      • 2020-06-15
      • 2023-01-05
      • 1970-01-01
      • 2012-11-02
      • 2015-08-13
      • 2023-03-04
      • 2020-06-15
      • 2021-11-15
      • 2016-09-08
      相关资源
      最近更新 更多