Koa2 request.body is empty

雨燕双飞 提交于 2019-12-08 11:58:45

问题


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?


回答1:


Adding Content-type: application/json header to my Postman request, resolved the issue.



来源:https://stackoverflow.com/questions/38856815/koa2-request-body-is-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!