I have a table in Dynamo
Now I am trying to add a new row(product) in the table. when I am trying this with API gateway, I created a resource and created a post method with request mode Product
Request Model
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"title" : "Error Schema",
"type" : "object",
"properties" : {
"ID" : { "type" : "string" },
"Catogory" : { "type" : "string" },
"SubCatogory" : { "type" : "string" },
"ProductName" : { "type" : "string" }
}
}
When I am trying to invoke lambda using API gateway,
{
"message": "Internal server error"
}
when I dig deep I am getting error as
Invalid type for parameter Item, value: {
"SubCatogory": "Car",
"ID": "6",
"ProductName": "jeep wrangler",
"Catogory": "Vehicle"
}, type: <class 'str'>, valid types: <class 'dict'>.
My request body looks like this.
{
"SubCatogory": "Car",
"ID": "6",
"ProductName": "jeep wrangler",
"Catogory": "Vehicle"
}
how to retrieve data and add in DB
Edit 1
Adding the Code used in Lambda
import json
import boto3
def lambda_handler(event, context):
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table('Products')
request_params = event['body']
project_json = request_params
response = table.put_item(Item=project_json)
# response = event['body']
return {
'statusCode': 200,
'body': response
}
when I am trying to returning event['body']
I can see the response as as same as inputs
EDIT 2
Have tried to convert json string to dict with json.loads(request_params)
import json
import boto3
def lambda_handler(event, context):
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table('Products')
request_params = event['body']
product_json = json.loads(request_params)
response = table.put_item(Item=product_json)
# response = json.loads(event['body'])
return {
'statusCode': 200,
'body': response
}
Getting error as
Execution failed due to configuration error: Malformed Lambda proxy response
There are a couple of problems in your code:
Item=json.loads(event['body'])
'body': json.dumps(response)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.