koajs / bodyparser

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

fetch post can't get body #93

Closed chenyulun closed 6 years ago

chenyulun commented 6 years ago
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 6 years ago

hi @chenyulun did you solve this issue?

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

KristerV commented 5 years ago

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 :)

KristerV commented 5 years ago

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