Lambda function error "list indices must be integers or slices, not str"

0

I have imported a zip file in my lambda function, but if i test it i get this error: "list indices must be integers or slices, not str". My code is this:

def lambda_handler(event, context):
query = {
    "size": 25,
    "query": {
        "multi_match": {
            "query": event['queryStringParameters']['q'],
            "fields": ["title", "directors"]
        }
    }
}

headers = {"Content-Type": "application/json"}

r = requests.get(url, auth=awsauth, headers=headers,
                 data=json.dumps(query))

response = {
    "statusCode": 200,
    "headers": {
        "Access-Control-Allow-Origin": '*'
    },
    "isBase64Encoded": False
}
response['body'] = r.text
return response

The error is on this line "query": event['queryStringParameters']['q'].

1 Answer
0
Accepted Answer

I'd recommend to print out the event object and understand whether the 'q' field is present or not.

For instance:

print('event:', json.dumps(event))
print('queryStringParameters:', json.dumps(event['queryStringParameters']))

Then you can verify whether some checks need to be done or defaulting to an empty string.

profile picture
EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions