fetch post can't get body
chenyulun opened this issue · comments
DylanNelly commented
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"}
AlexanderTserkovniy commented
hi @chenyulun did you solve this issue?
Have it right now, cannot get what's wrong.
Krister Viirsaar commented
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 :)
Krister Viirsaar commented
My solution was to swith to koa-body
instead. That works perfectly.