koajs / bodyparser

Koa body parsing middleware
MIT License
1.3k stars 116 forks source link

Can resolve to normal parameter type #118

Closed yoonasy closed 5 years ago

yoonasy commented 5 years ago

set request

content-type': 'application/x-www-form-urlencoded; charset=UTF-8

image

koa

app.use(cors())
app.use(bodyParser())
app.use(registerRouter())

...
router.all('/addItem', async (ctx, next) => {
  let { parentID, type, content } = ctx.request.body
  console.log(ctx.request.body)
  ctx.body = ''
})

result

{
  parentID: '1',
  type: '0',
  content:
  {
    isExpand: 'true',
    name: '测试'
  }
}

The isExpand type should be a Boolean value,But it‘s strings,Excuse me, what should I do

dead-horse commented 5 years ago

if you want to keep params' type, you should use application/json format. application/x-www-form-urlencoded is a string base protocol, it can't keep params' type

yoonasy commented 5 years ago

@dead-horse

超级感谢大佬 解决了

image

是我axiosdata转换成query参数了 qs.stringfy(data)

之前用的.net后台 他们后台能直接解析 想着bodyparser也能解析

image