2 Answers
- Newest
- Most votes
- Most comments
0
The event is already received by the lambda function as python dictionary so you do not need to use json.loads() to parse the event.
What you are probably trying to achieve is to parse the POST json body from the event and convert it from json string to python dictionary. So you need to do something like this:
body = json.loads(event["body"])
0
This works for me:
import json
import base64
# import requests
def lambda_handler(event, context):
#print out the body of the request
print(event['body'])
try:
# Decode the Base64 encoded body
decoded_body = base64.b64decode(event['body']).decode('utf-8')
except (ValueError, UnicodeDecodeError):
# Handle decoding errors
decoded_body = 'Error: Invalid Base64 encoded body'
return {
"statusCode": 200,
"body": json.dumps({
"message": decoded_body,
}),
}
and then when I test it:
curl https://abc123.execute-api.af-south-1.amazonaws.com/Prod/create -X POST -d '{"key1":"value1", "key2":"value2"}'
{"message": "{\"key1\":\"value1\", \"key2\":\"value2\"}"}%
answered 12 days ago
Relevant content
- Accepted Answerasked 13 days ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 2 years ago