2

I am using Amazon API Gateway to invoke a lambda function. I am testing a get request with the following as my query string earlyDate="12-01-21"&laterDate="12-03-21".

I currently have my lambda function returning the event that gets passed:

def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "body": json.dumps(event)
    } 

This works as expected. When I test my API gateway I get a response which includes

"queryStringParameters": {
    "earlyDate": "12-01-21",
    "laterDate": "12-03-21"
  },
  "multiValueQueryStringParameters": {
    "earlyDate": [
      "12-01-21"
    ],
    "laterDate": [
      "12-03-21"
    ]
  },

This indicates I should be able to access these query parameters at event.queryStringParameters. However when I change my lambda function to return those:

def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "body": json.dumps(event.queryStringParameters)
    } 

The result is a 502 error.

How do I access the query string parameters passed in from my API Gateway?

1 Answer 1

2

I think, event is a dict and we can access its query params as event['queryStringParameters']

We can confirm that by

for key, value in event.items():
   print(key, value)

Lambda could return this:

return {
    'statusCode': 200,
    'body': json.dumps(event['queryStringParameters'])
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.