In the last lecture, I gave you a hint that there is this language, this templating language we can use
here to extract data from the request we have and define which data we want to forward.
Now actually, you can learn more about this language if you Google for AWS API Gateway body mapping
templates.
The first article you find here below the ads gives you an overview over this language,
the special variables you can use in this body mapping template text box there and how you can retrieve
data. This is a good place to dive deeper
but you'll also see quite some examples throughout this course.
A quick example can be found if you go to the drop-down and choose method request pass through.
What this does is it gives you a quite elaborate template, you might not understand all of that I can
tell you it uses Apache's Velocity language and it basically extracts a lot of data from the request,
data from the request body
but also a lot of metadata.
If we save this and I will come back to explaining this later and we go back to testing this, you will
still see null because our lambda code still tries to access an age property which doesn't exist on event
but if we go to our cloud watch logs here and we wait for the latest log to arrive, you will see that
now we log a big object which has a body JSON property which holds our actual request body,
now not as a JSON string but already as a Javascript object,
all of this is a Javascript object and a lot of other information about this request. So kind of similar
to the time we use this checkbox, use this proxy integration and forward everything
but now not forwarding the request but forwarding a mapped template or a mapped event data object with
data from that request which is also the reason why we wouldn't have to parse JSON data here because
it already is a mapped Javascript object instead
and that's the power.
Also, this doesn't override our integration response,
we're still in the world of API Gateway doing the request and response handling,
we just happen to extract a lot of data from the request and pass it to the action
but we can narrow this down.
We can go to our template and get rid of all the stuff at the bottom here,
a lot of information we don't need about authorization and so on, we'll need this later though and only extract
data from the request body
and this is done with this special command, input JSON and then dollar sign.
This is dollar sign, a special variable which refers to your request body and JSON simply parses
it and with that, you can also of course rename this to age and not parse your full request body but there,
person data, you know that is a property on the request body you're parsing and then age. And now if
we remove this comma, important that you don't have a syntax error,
now if we save this and go back to testing this, if we hit test here, you'll see 56 again
and important too if you check your cloud watch logs, wait for the latest one to arrive
again, you'll see that we only received this object.
So all the other data was dropped,
we have no other metadata about the requests because in our mapping template under integration request
here, we only extract that
and the difficult part here to grasp probably is the language we use with this $input.json('$')
thing.
So let's dive deeper into this in the next lecture.