Ogg
Ogg

Reputation: 59

How to map path parameter which is not at path end using Mapping Template when using AWS API Gateway with AWS Lambda?

Assuming a route like this is defined in the API Gateway:

POST /projects/{projectid}/key

which is mapped to an AWS Lambda,

how could one get that {projectid} value in the Lambda handler (i.e. as a field of the input object which is a custom Java class)?

As far as i understood, something like this should do the job: In the API Gateway definition, Integration Request > Mapping template i've added the following for the content-type "application/json":

{ "projectid": "$input.params('projectid')" }

However, even if the part after the colon ($input.params('projectid) seems to be picked-up correctly (in the API Gateway logs from CloudWatch the value looks as expected), the mapping is not working: null value reaches the backend.

Please note that a similar approach for a route like

GET /projects/{id} works as expected with similar mapping.

Looks like the path param only gets mapped if it is the last one in the path?

Upvotes: 1

Views: 984

Answers (2)

Ogg
Ogg

Reputation: 59

I've found the problem. It was not the Mapping Template itself, but the way i was invoking the AWS SDK function. Specifically:

apigClient.projectsProjectidKeyPost(params, {}, {})

does not work - projectid arives null in the Lambda code.

However, this works (please note the change in the 2nd argument value):

apigClient.projectsProjectidKeyPost(params, '{}', {})

Not sure what exactly is the root cause of this behavior though.

Upvotes: 2

jackko
jackko

Reputation: 7344

  1. Check your Java Lambda handler to make sure the deserialization is handled correctly. You could start with a simple NodeJS function for testing that would not require a RequestHandler implementation (it will deserialize everything in the JSON payload without any handler configuration).

  2. Not sure if you're talking about the same API here, but you should not be allowed to have two path parameters on the same segment, i.e. /projects/{id} and /projects/{projectid} should not coexist in the same API.

  3. You could start with using $input.params() to get all your parameters and confirm that 'projectid' is coming through in that map. Again, this would be easier to debug in a NodeJS function.

Upvotes: 1

Related Questions