koa-body和koa-bodyparser post 请求时ctx.request.body 获取到的参数为空

目前的问题是我分别使用了kao\-bodyparser和koa\-body的中间件来获取post请求的数据,都显示为空对象,Content\-type 为x\-www\-form\-urlencoded和json都不行,只有get可以,有大佬知道是为什么吗?
我用原生的方式解析是可以拿到的

package.json

{
  "name": "koa-server",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "server": "tsc && nodemon src/lib/www.ts",
    "watch": "cross-env nodemon --watch 'src/**/*' -e ts,tsx --exec 'ts-node' src/lib/www.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "koa": "^2.13.0",
    "koa-bodyparser": "^4.3.0",
    "koa-json": "^2.0.2",
    "koa-logger": "^3.2.1",
    "koa-onerror": "^4.1.0",
    "koa-router": "^10.0.0",
    "koa-static": "^5.0.0",
    "mongoose": "^5.10.13"
  },
  "devDependencies": {
    "@types/koa": "^2.11.6",
    "@types/koa-router": "^7.4.1",
    "cross-env": "^7.0.2",
    "nodemon": "^2.0.6",
    "ts-node": "^9.0.0",
    "typescript": "^4.0.5"
  }
}

app.ts

const bodyparser = require("koa-bodyparser");
// 首页api路由
const Index = require("./router/index");
// 异常中间件
error(app);
// 配置post请求传回参数
app.use(bodyparser());
app.use(async (ctx: any, next: Function) => {
    console.log(ctx.request.body);
    await next();
})

axios配置

instance.defaults.headers.post["Content-Type"] =
    "application/x-www-form-urlencoded;charset=utf-8;";

请求方法

 test: async ()=>{
        await post("/test",{name:"测试",value:"123"});
      },
阅读 3.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进