koajs / bodyparser

Koa body parsing middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fetch post can't get body

chenyulun opened this issue · comments

fetch('http://127.0.0.1:3000/',{method: 'POST',headers:{"Content-Type":"application/json; charset=UTF-8"},body:JSON.stringify({user:'people'})})

ctx.body // undefined

fetch('http://127.0.0.1:3030/',{method: 'POST',headers:{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"},body:'user=people'}) 
curl -X POST http://localhost:3000/ --data '{"user":"people"}' --header 'Content-Type:application/json; charset=UTF-8'

ctx.body // {"user":"people"}

hi @chenyulun did you solve this issue?

Have it right now, cannot get what's wrong.

ctx.body is the body that gets returned to client (response.body). What you need is ctx.request.body. In my case however that also returns empty :)

My solution was to swith to koa-body instead. That works perfectly.