I'm encountering difficulties with extracting request data in a Flask-Lambda application deployed on AWS Lambda using AWS SAM (Serverless Application Model). Here's a breakdown of my concerns:
Background:
I've developed a Flask-Lambda application intended to handle various HTTP requests, including POST requests, deployed on AWS Lambda using AWS SAM.
Initially, I used SAM CLI to create a basic "hello_world" template, and I successfully built and deployed the application. GET requests function as expected.
Issue:
However, I'm facing challenges with extracting request data specifically in POST requests.
When attempting to retrieve the request body using methods like
request.form.to_dict(),request.data, orrequest.get_json(), I consistently receive either empty dictionaries or encounter exceptions.
Troubleshooting Steps Taken:
I've verified that the Lambda function is properly configured to handle POST requests.
The API Gateway integration request mapping templates appear to be set up correctly to pass the request body to the Lambda function.
I've ensured that Flask-Lambda is integrated correctly within my application and that routes are defined accurately.
Additionally, I've attempted to debug the issue by introducing logging statements in the Lambda function.
Request for Assistance:
Despite these efforts, I'm still unable to extract the request data successfully.
I'm seeking guidance on potential solutions or further debugging steps to resolve this issue and successfully retrieve request data in POST requests within my Flask-Lambda application deployed on AWS Lambda via AWS SAM.
Sample Input :
{
"id":"1",
"name":"Bob",
"age":"40"
}
Sample Code
import json
import boto3
from flask_lambda import FlaskLambda
from flask import request, jsonify
app = FlaskLambda(__name__)
ddb = boto3.resource('dynamodb')
table = ddb.Table('mytable')
@app.route('/')
def healthCheck():
data = {
"message":"Healthy"
}
return (
json.dumps(data),
200,
{"Content-Type":"application/json"}
)
@app.route('/leads', methods=["GET","POST"])
def list_or_add_leads():
if request.method == 'GET':
data = table.scan()['Items']
return (json.dumps(data), 200, {"Content-Type":"application/json"})
elif request.method == 'POST':
try:
# Extract raw request body
print(event)
print(request.body)
# table.put_item(Item=request_body)
data = {
"message":"Successful"
}
return (json.dumps(data), 200, {"Content-Type":"application/json"})
except Exception as e:
data = {"exception":str(e)}
return (json.dumps(data), 400, {"Content-Type":"application/json"})