CodexBloom - Programming Q&A Platform

AWS Lambda Unpacking Event Payload guide with API Gateway Proxy Integration

๐Ÿ‘€ Views: 102 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-12
aws lambda api-gateway json base64 python

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!