AWS Lambda Unpacking Event Payload guide with API Gateway Proxy Integration
After trying multiple solutions online, I still can't figure this out. I'm having a hard time understanding I'm getting frustrated with I'm a bit lost with I'm working with an scenario with my AWS Lambda function that is integrated with API Gateway using proxy integration. The Lambda function is supposed to process a JSON payload sent via a POST request, but I keep getting an incorrect event structure that seems to not include the details I expect. Hereโs a simplified version of the Lambda function: ```python import json def lambda_handler(event, context): print("Event: ", json.dumps(event)) body = event['body'] data = json.loads(body) return { 'statusCode': 200, 'body': json.dumps({'message': 'Success', 'data': data}) } ``` I've set up my API Gateway to send a JSON payload, similar to this: ```json { "name": "John", "age": 30 } ``` However, when I test the API Gateway through the console, the event being logged in my Lambda function appears like this: ```json { "isBase64Encoded": false, "statusCode": 200, "headers": {}, "body": "eyJuYW1lIjoiSm9obiIsImFnZSI6MzB9", "httpMethod": "POST", "path": "/myapi", "requestContext": {...} } ``` The payload is being base64 encoded, and Iโm unsure how to decode it properly to get to my original JSON object. Iโve tried decoding the body like this: ```python if event['isBase64Encoded']: body = base64.b64decode(event['body']).decode('utf-8') else: body = event['body'] ``` However, it still results in an behavior when trying to parse the JSON. The behavior returned is `JSONDecodeError: Expecting value: line 1 column 1 (char 0)`. Iโve double-checked that the input from the API Gateway is set to application/json, but Iโm unsure what I'm missing. Any insights on resolving this scenario and correctly parsing the event payload would be greatly appreciated. Is there a specific configuration I need to adjust in API Gateway to prevent the base64 encoding, or is my Lambda function incorrectly handling the event? I'm using Python stable in this project. I'm using Python 3.11 in this project. Is there a simpler solution I'm overlooking? My team is using Python for this microservice. Thanks, I really appreciate it!