Skip to content

Desirable to be able to configure the name of ctx.request.body #39

@ztrange

Description

@ztrange

Right now, I can do this and access body, files and fields

var Koa = require('koa')
var body = require('koa-better-body')
var convert = require('koa-convert')

var app = new Koa()

app
  .use(convert(body()))
  .use(async function (ctx) {
    console.log(ctx.request.body)    // if buffer or text
    console.log(ctx.request.files)   // if multipart or urlencoded
    console.log(ctx.request.fields)  // if json
  })
  .listen(8080, function () {
    console.log('koa server start listening on port 8080')
  })

But if I want to use the property named body to access the content of a json, I'd do the following:

...
app
  .use(convert(body({
    fields: 'body', // Now body contains the incoming json
    files: 'uploads', // I can even rename files
    body: 'buffer' // BUT THIS I CAN'T DO 
  })))
  .use(async function (ctx) {
    console.log(ctx.request.buffer)    // if buffer or text - THIS WON'T WORK
    console.log(ctx.request.uploads)   // if multipart or urlencoded
    console.log(ctx.request.body)  // if json
  })
...

I'd like to be able to rename the buffer/text property to something else.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pkg: koa-better-bodyPriority: MediumThis issue may be useful, and needs some attention.Status: AcceptedIt's clear what the subject of the issue is about, and what the resolution should be.Type: EnhancementMost issues will probably be for additions or changes. Expected that this will result in a PR.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions