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개 답변
0
수락된 답변

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
전문가
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠