I am trying to test a lambda function locally, using serverless framework. This function is attached to a POST endpoint, defined as:
createCat:
handler: app/handler.createCat
events:
- http:
path: cats/
method: post
I invoke the function locally a according to this source:
# Command line
serverless invoke local --function createCat --path local-invoke/createCat.json --stage local
# createCat.json
{
"body": {
"name": "Cat cat blue",
"age": 4,
"color": "blue"
}
}
I am trying to parse the body and get the items like it is shown here, but it does not work:
import { Context } from 'aws-lambda';
const createCat = async (event: any, context?: Context) => {
try{
const data = JSON.parse(event.body)
let name = data?.name
let age = Number(data?.age)
let color = data?.color
return {
statusCode: 500,
message: data
}
}
catch(error) {
return {
statusCode: 500,
body: JSON.stringify({
message: getMessageFromError(error)
})
}
}
}
I am getting this output, instead of the parsed body:
{
"statusCode": 500,
"body": "{\"message\":\"Unexpected token l in JSON at position 0\"}"
}
Serverless documentation on local invoke does not touch on how to pass/retrieve body params either.
What am I doing wrong here?