vinod827
vinod827

Reputation: 1524

How to pass the query string parameters in the AWS API Gateway?

Following is my Lambda handler which is expecting the users data from the queryStringParameters:-

export const lambdaHandler = async (event, context) => {
    try {
        const numberOfUsersRequested= (event && event.queryStringParameters.users) ? event.queryStringParameters.users : 10;
        const users = await generateUsers(numberOfUsersRequested).then(data => data.users);

I'm using AWS SAM to develop my Lambda and I can test it very well using the event.json as an input event to this Lambda locally. Here is a chunk of event.json where I'm passing the queryStringParamters users like this:-

{
  "body": "{\"message\": \"mock data\"}",
  "resource": "/{proxy+}",
  "path": "/path/to/resource",
  "httpMethod": "POST",
  "isBase64Encoded": false,
  "queryStringParameters": {
    "users": 200
  },

Now, may I know how to pass the same QueryStringParameters from the AWS API Gateway console. Currently, I'm getting this 500 error on the AWS console for the API Gateway:-

{
  "message": "Internal server error"
}

Mon Sep 28 01:24:15 UTC 2020 : Endpoint response headers: {Date=Mon, 28 Sep 2020 01:24:15 GMT, Content-Type=application/json, Content-Length=2, Connection=keep-alive, x-amzn-RequestId=0e1f110c-e80c-4ff1-870a-5cafd04167db, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5f713b3d-4762f9b07ee8c1d7c6623574;sampled=0}
Mon Sep 28 01:24:15 UTC 2020 : Endpoint response body before transformations: {}
Mon Sep 28 01:24:15 UTC 2020 : Execution failed due to configuration error: Output mapping refers to an invalid method response: 200
Mon Sep 28 01:24:15 UTC 2020 : Method completed with status: 500

I have performed the following steps to mitigate the issue but not working. It looks like something is missing :- 1)Added the url query string parameters as users in the Method Request (refer screenshot)

enter image description here

  1. In the Integration Request -> Mapping Templates, added the mapping as application/json:-

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

enter image description here

enter image description here

enter image description here

  1. And finally passing the query string as users=6.

Upvotes: 2

Views: 9878

Answers (1)

Marcin
Marcin

Reputation: 238061

In your case, the event should just be:

{
    "users": "6"
}

You can add the following to the beginning of your handler to confirm:

  console.log(JSON.stringify(event, null, 2));

Therefore, to get users value you should just use event.users, not event.queryStringParameters.users.

Upvotes: 2

Related Questions