Aws Api Gateway Integration Request How to append a property to request body?

柔情痞子 提交于 2019-12-02 14:03:24

问题


I want to combine request body and querystring parameters before sending it to lambda. Let's say I have an entity in Lambda as below :

Class Person {
private String firstName;
private String lastName;
private String language;
}

And the json which sent to api gateway is{"firstName":"Foo","lastName":"Bar"} As you see "language" field is missing in request body. I want to get this language field from querystring and add to json. How can I achieve tihs ?

Is there a way to do in integration request section ? For example :

$input.json(x).append("language":"$input.params('name')")

I could not find any valuable information. Thanks in advance.


回答1:


You can use body mapping template in the integration request section and get request body and query strings. Construct a new JSON at body mapping template, which will have data from request body and query string. As we are adding body mapping template your business logic will get the JSON we have constructed at body mapping template.

Inside body mapping template to get query string please do ,

$input.params('querystringkey')

For example inside body mapping template,

#set($inputRoot = $input.path('$'))
{
"firstName" : "$input.path('$.firstName')",
"lastName" : "$input.path('$.lastName')"
"language" : "$input.params('$.language')"
}

Please read https://aws.amazon.com/blogs/compute/tag/mapping-templates/ for more details on body mapping template



来源:https://stackoverflow.com/questions/46407161/aws-api-gateway-integration-request-how-to-append-a-property-to-request-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!