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 Risposta
0
Risposta accettata

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
ESPERTO
con risposta un anno fa

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande