search

Home  >  Q&A  >  body text

javascript - Please answer, the parameter obtained by koa-bodyparser is an empty object, why? ? ? ? ?

code show as below

const Koa = require('koa');
const app = new Koa();
const router = require('koa-router')();
const bodyParser = require('koa-bodyparser');
app.use(bodyParser());


router.post('/test', async function (ctx, next) {
  ctx.body = ctx.request;
  console.log(`我是router中的=====> ${JSON.stringify(ctx.request.body)}`);
    // 这里得到是一个空对象
});

app.use(router.routes());

app.listen(3000)

Then I requested this interface and what was returned to me was an empty object. Please give me an answer {}

欧阳克欧阳克2703 days ago781

reply all(2)I'll reply

  • 阿神

    阿神2017-07-05 10:42:07

    1. Pay attention to the content-type sent by postman each time. Application/x-www-form-urlencoded or application/json

    2. If it is json, the json written using raw must be in a standard format, with double quotes for the key and double quotes for the value string.

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-07-05 10:42:07

    Try ctx.request.fields

    reply
    0
  • Cancelreply