1

I have an API that is connected to a lambda function that has queryStringParameters. On the function end I have

VARIABLE = event['queryStringParameters']['variable']

When I deploy my API and try to use it "api_url"?variable=something,

I get {"message": "Internal server error"}.

In Cloudwatch I get:

[ERROR] KeyError: 'queryStringParameters'

Traceback (most recent call last):

File "/var/task/index.py", line 217, in handler

VARIABLE = event['queryStringParameters']['variable']

To help troubleshoot I print the event. In Cloudwatch it does, and it appears as "{}", so pretty much as empty.

When I test the function in the console I use the event:

{ "queryStringParameters": {"variable": "T"}}

and the function works just fine.

I've made APIs that are connected to lambda functions before almost identical to this and have had no problem. I'm stumped. Any advice is appreciated.

1 Answers1

0

Based on this AWS guide, you can access the query string parameters using get methods of the event object.


query_string = event.get('queryStringParameters')

if query_string is not None:
  name = query_string.get('variable')

In your lambda's case, its quite strange that no queryStringParameters key could be found on the event object. It would be interesting to see if there's an actual parameter sent to the REST API or if the positioning of parameters in the handler's parameters are correct and that the parameter is provided on all requests.


def event_handler(event, context): # Make sure event goes first
  query_string = event.get('queryStringParameters')

  if query_string is None:
   // Return 400 / 422 here to indicate a bad request
Allan Chua
  • 9,305
  • 9
  • 41
  • 61