Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get POST params? #719

Closed
jianxiangxun opened this issue Apr 28, 2016 · 7 comments
Closed

How to get POST params? #719

jianxiangxun opened this issue Apr 28, 2016 · 7 comments

Comments

@jianxiangxun
Copy link

koa: v2.0.0
koa-router: v7.0.1

// user
router
.get('/user/:id', async (ctx, next) => {
    let user = await User.query();
    ctx.body = user;
}) 
.post('/user/', async (ctx, next) => {
    ctx.body = ctx.req;
 })

Everything seems all right, but I can't find the post params.

Insert the code:

router
.get('/user/:id', async (ctx, next) => {
    let user = await User.query();
    ctx.body = user;
})
.post('/user/', async (ctx, next) => {
    var data = '';
    ctx.on('data', function (chunk) {
        data += chunk;
    })
    ctx.on('end', function (chunk) {
        console.log(data);
    })
    ctx.body = ctx.req;
})

The data will be print correctly!
I don't want to use this approach.
Have any good Suggestions?

@sibelius
Copy link

@xunjianxiang you can get your POST params like this:

ctx.request.body // your POST params
ctx.params // URL params, like :id

@hanxu317317
Copy link

I get ctx.request.body but it's undefined

@fl0w
Copy link
Contributor

fl0w commented Aug 2, 2017

@hanxu317317 you need a body parser. Google for koa-bodyparser and/or koa-body.

@hanxu317317
Copy link

thank you, it's ok

@KevinHe666
Copy link

yeah

@yuhr
Copy link

yuhr commented May 5, 2018

yes

@macizomedia
Copy link

Hi, does body parser required some extra config, I still get an empty array using both ctx.request.body and ctx.params. Im also using koa-joi-router. I want to access the verification code from params.

thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants