I've been struggling to parse the request body from POST. I tested the code locally via curl and returns the json body. On Lambda, I tried both curl and Postman but it just returned the buffer data.
Basically, I can't manipulate the data.
index.js
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const serverless = require('serverless-http');
const app = express();
app.use(express.json());
// app.use(bodyParser.json());
// Routes
app.use('/db', require('./routes/db'));
module.exports.handler = serverless(app);
routes > db.js
const express = require('express');
const { Pool } = require('pg');
const router = express.Router();
// Test
router.post('/test', (req, res) => {
console.log('Raw req.body:', req.body);
res.json({ received: req.body });
});
module.exports = router;
Sample request:
{
"movetype": 1
}
This is the response using Postman:
{
"received": {
"type": "Buffer",
"data": [
123,
13,
10,
32,
32,
34,
109,
111,
118,
101,
116,
121,
112,
101,
34,
58,
32,
49,
13,
10,
125,
13,
10
]
}
}