2 回答
- 最新
- 投票最多
- 评论最多
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\"}"}%
已回答 2 个月前
相关内容
- AWS 官方已更新 1 年前
- AWS 官方已更新 2 年前
- AWS 官方已更新 2 年前
- AWS 官方已更新 2 年前