Reputation: 1225
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.
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]"}
I want receive the data from postman by x-www-formurlencoded.
How I can do it?
If you know of it, please help me.
Upvotes: 1
Views: 592
Reputation: 3313
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
Upvotes: 0