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 Resposta
0
Resposta aceita

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
ESPECIALISTA
respondido há um ano

Você não está conectado. Fazer login para postar uma resposta.

Uma boa resposta responde claramente à pergunta, dá feedback construtivo e incentiva o crescimento profissional de quem perguntou.

Diretrizes para responder a perguntas