Koa2 request.body is empty
--- This question is pretty old and libraries used here are most likely outdated, the solution is still correct, but please use up-to-date versons ---
I am working on a web service with koa2 and node6. My koa dependencies as follows;
"koa": "^2.0.0-alpha.4",
"koa-async-body": "^1.0.4",
"koa-bodyparser": "^3.2.0",
"koa-logger": "^1.3.0",
"koa-router": "^7.0.1",
My implementation is like this;
const apiPrefix = 'api',
apiParent = 'auth',
api = 'register',
router = new Router();
router.prefix(`/${apiPrefix}/${apiParent}/${api}`);
router.post('/', async(context, next) => {
try {
console.log(context.request.body);
context.body = await post(context.request.body);
await next();
} catch (err) {
context.throw(500);
}
});
In another class, I bind this route to app. Also I have added bodyParser to the Koa as follows;
const app = new Koa();
app.use(bodyParser());
When I try to log the request body, it's an empty object. On the other hand, this setup works fine with other people in this project.
What am I doing wrong? Am I using an outdated dependency?
Adding Content-type: application/json
header to my Postman request, resolved the issue.