简体   繁体   中英

AWS API Gateway Overwriting Integration Request Mapping Template

It appears that when I send a body with a POST request my template mapping is getting overwritten by the payload.

I have an API gateway resource invoking a Lambda function. The Lambda function is simple, it just echoes what it receives.

exports.handler = function(event, context) {
    context.succeed({event: event});
};

This Lambda function, called echo, is invoking the Lambda function through a POST. The POST method has an integration request mapping template (which is straight from the docs ):

{
    "name" : "$input.params('name')",
    "body" : $input.json('$')
}

When I make a request WITHOUT a body I get back the response I expect:

curl -XPOST https://foo.execute-api.us-east-1.amazonaws.com/test/echo?name=foo

{"event":{"name":"foo","body":{}}

However, when I make a request WITH a body the template mapping no longer works:

curl -XPOST https://foo.execute-api.us-east-1.amazonaws.com/test/echo?name=foo -d '{"text": "Say goodbye to your template mapping"}'

{"event":{"text":"Say goodbye to your template mapping"}

How can I ensure my template mapping applies when a body is present in the request?

Mapping templates are bound to a request by the content type of the request. In this case the template mapping was for the content type 'application/json'. By including the Content-Type: application/json header in the request the mapping template is applied.

This returns the expected result:

curl -XPOST https://qb9p3d1ovf.execute-api.us-east-1.amazonaws.com/test/echo?name=foo -H "Content-Type: application/json" -d '{"text": "Say goodbye to your template mapping"}'

{"event":{"name":"foo","body":{"text":"Say HELLO to your template mapping"}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM