hi,
I am quite puzzled by this issue: the lambda function receive the POST request from the API with the correct event object (it has a body, requestContext, etc.).
handler:
exports.handler = (event, context) => {
console.log(`EVENT: ${JSON.stringify(event)}`);
awsServerlessExpress.proxy(server, event, context);
};
Express:
var app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(awsServerlessExpressMiddleware.eventContext());
CloudWatch is properly showing body as stringified JSON. However express route doesn't get the body anymore (it does properly get req.apiGateway.event.requestContext).
JSON diff shows that only body has disappeared between the lambda handler receiving the event and the processing by awsServerlessExpress.proxy().
The POST request is made though the AWS API:
let apiName = 'APINAME';
let path = '/myroute';
let myInit = {
headers: {
'Content-Type': 'application/json;charset=UTF-8'
},
body: {
test: "test"
}
};
API.post(apiName, path, myInit)
.then((response) => {
console.log(response);
})
.catch((error) => {
console.log(error.response);
});
I tried what was suggested by this issue and also the example code without success.
What's even more puzzling, all the code is coming from a fresh start with AWS Amplify CLI, so I assumed it would be working out of the box.
anyone has a suggestion?
thanks!!
Can you show how you're defining your route and trying to access body?
oh man... this is embarrassing... I spent so much time on the API config thinking something was wrong with it or the proxy...
I was actually reading req.apiGateway.event.body instead of simply req.body....
I'm very sorry for the trouble :face_with_head_bandage: , there is no problem getting the body indeed.
thanks for putting up with this. The way to access the body was indeed incorrect!
Remember to use bodyParser.json() or bodyParser.urlencoded({ extended: false }) as express middleware to make sure the body is available in the req object.
Most helpful comment
Remember to use
bodyParser.json()orbodyParser.urlencoded({ extended: false })as express middleware to make sure the body is available in the req object.