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 Respuesta
0
Respuesta aceptada

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
EXPERTO
respondido hace un año

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas