9

Within my lambda function, which takes in event api query strings, I want to check if one is present. The below works if it is:

if event['queryStringParameters']['order'] == 'desc':
        file_names.append('hello')

I have tried event['queryStringParameters']['order'] != null but if there is no order query string used the lambda function the function breaks causing a 502 response. How do I check if a query string is not used without it breaking?

2 Answers 2

15

Always check if the dict contains an key before referencing it.

if 'queryStringParameters' in event and 'order' in event['queryStringParameters']:
Sign up to request clarification or add additional context in comments.

1 Comment

fantastic! That's it :)
3

I successfully use the dictionary.get method as in:

event['queryStringParameters'].get('param1')

or with a default value:

event['queryStringParameters'].get('param1', '')

Check out the syntax: https://www.w3schools.com/python/ref_dictionary_get.asp

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.