1

I send the parameter to api gateway by postman and receive the result. So I try simple example in lambda,

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

    callback(null, Idx);

};

when I send Idx, It callback double of it.

enter image description here

I select raw in Body and doing that I can receive the result

But, I put "Content-Type" : "application/x-www-form-urlencoded" in Headers part, select x-www-form-urlencoded, it returns this.

{"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]"}

enter image description here

I want receive the data from postman by x-www-formurlencoded.

How I can do it?

If you know of it, please help me.

Arafat Nalkhande
  • 11,078
  • 9
  • 39
  • 63
Polaris Nation
  • 1,085
  • 2
  • 18
  • 49

1 Answers1

0

API Gateway is expecting JSON, but you're giving it urlencoded form data (i.e. a string). You need to add a mapping template in your API Gateway configuration:

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

Source: https://stackoverflow.com/a/37956390/5794667

andydavies
  • 3,081
  • 4
  • 29
  • 35