Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS / Python Lambda function checking if a query string is present

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?

like image 329
5pence Avatar asked Sep 06 '18 13:09

5pence


People also ask

Can Lambda function have if statement?

Using if else & elif in lambda function We can also use nested if, if-else in lambda function.


2 Answers

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

if 'queryStringParameters' in event and 'order' in event['queryStringParameters']:
like image 165
John Hanley Avatar answered Oct 04 '22 02:10

John Hanley


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

like image 25
devplayer Avatar answered Oct 04 '22 02:10

devplayer