how pass data from body in koa js

router.post("/api/comments", async ctx => {
  ctx.session.comments = ctx.session.comments || [];

  if (!ctx.request.body["comment"]) {
   throw Boom.badData("Empty comments not allowed");
  }

  const comment = {
   date: new Date(),
   comment: ctx.request.body["comment"],
  };
  ctx.session.comments.push(comment);
  ctx.status = 201;
  ctx.body = comment;
 });

4
4
Mahdi_12167 120 points

                                    httpRouter.post('/syncCallJS/*', async (ctx, next) => {
 const idx = syncCallJSIndex++
 const channelId = ctx.params[0]
 const payload = ctx.request.body
 const device = Device.getDevice(channelId)
 if (device) {
  const terminal = new SyncTerminal()
  let data
  terminal.channelId = channelId
  syncV8Hub.join(terminal, false)
  payload.params.syncId = 100000 + idx
  payload.id = 100000 + idx
  data = await terminal.send(payload)
  ctx.response.status = 200
  ctx.type = 'application/json'
  ctx.response.body = JSON.stringify(data.ret)
 } else {
  ctx.response.status = 500
 }
 await next()
})

4 (4 Votes)
0
0
0
Kanch 105 points

                                     ctx.throw(400, 'Grant: mount session middleware first')
if (ctx.method === 'POST' && !ctx.request.body) {
 ctx.throw(400, 'Grant: mount body parser middleware first')
  ctx.session.grant.override = override
 if (Object.keys(ctx.request.body || {}).length) {
  ctx.session.grant.dynamic = ctx.request.body

0
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source