question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get POST params?

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

187reactions
sibeliuscommented, Apr 28, 2016

@xunjianxiang you can get your POST params like this:

ctx.request.body // your POST params
ctx.params // URL params, like :id
60reactions
fl0wcommented, Aug 2, 2017

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How are parameters sent in an HTTP POST request?
Short answer: in POST requests, values are sent in the "body" of the request. With web- ...
Read more >
How To Retrieve URL and POST Parameters with Express
First, open your terminal window and create a new project directory: mkdir express-params-example. … Step 2 – Using req. query with URL ...
Read more >
Get / Post Parameters - Dotcom-Monitor
Name: Specify the name of the parameter as it will appear in the request. Value: Enter the value associated with the name of...
Read more >
How to retrieve the POST query parameters using Express
Retrieve the POST query parameters using Express ... POST query parameters are sent by HTTP clients for example by forms, or when performing...
Read more >
How to Send GET and POST Requests with JavaScript Fetch ...
Creating get and post Functions ... // All logic is here. };const get = ( url, params ) => request( url, params, 'GET'...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found