3

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.

Mesut Dogan
  • 572
  • 7
  • 16

1 Answers1

2

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

Vijayanath Viswanathan
  • 8,027
  • 3
  • 25
  • 43
  • 2
    I do not want to construct new json because if I do it I have to change an entity in Lambda. Thanks. – Mesut Dogan Sep 25 '17 at 14:35
  • You can add the complete enitity in body mapping template without any change in the entity structure. Or are you happy to get query string directly in code? I have change my answer a bit. – Vijayanath Viswanathan Sep 25 '17 at 14:39