I want to read the "key" parameter of a http post request but it is not working.
def my_handler(event, context):
print(event)
print(event['body'])
print("key: " + event['key'])
key = event['query']['key']
encoded_string = str(key).encode("utf-8")
# Create the file named for example "42.json" containing the appropriate data
s3_path = str(key) + '.json'
s3 = boto3.resource("s3")
s3.Bucket(BUCKET_NAME).put_object(Key=s3_path, Body=encoded_string)
message = {
'message': 'Created {}!'.format(key)
}
return {
'statusCode': 200,
'headers': {'Content-Type': 'application/json'},
'body': json.dumps(message)
}
Update: If I use the code below, I can read JSON data in an http post but I still cannot read form-data.
def my_handler(event, context):
print(event)
print(event['body'])
# print("key: " + event['key'])
print("key " + json.loads(event['body'])["key"])
key = json.loads(event['body'])["key"]
encoded_string = str(key).encode("utf-8")
# Create the file named for example "42.json" containing the appropriate data
s3_path = str(key) + '.json'
s3 = boto3.resource("s3")
s3.Bucket(BUCKET_NAME).put_object(Key=s3_path, Body=encoded_string)
message = {
'message': 'Created {}!'.format(key)
}
return {
'statusCode': 200,
'headers': {'Content-Type': 'application/json'},
'body': json.dumps(message)
}
