koa middleware to parse xml request body
npm install koa-xml-body




> Parse xml request body for Koa

``js
const koa = require('koa')
const xmlParser = require('koa-xml-body')
const app = koa()
app.use(xmlParser())
app.use(function(ctx, next) {
// the parsed body will store in this.request.body
// if nothing was parsed, body will be undefined
ctx.body = ctx.request.body
return next()
})
`
koa-xml-body will carefully check and set context.request.body, so it can intergate well with other body parsers such as koa-bodyparser:
`js
// ...
const bodyParser = require('koa-bodyparser')
// ...
app.use(xmlParser())
app.use(bodyParser())
`
Note:
- For koa@2.x, use version 2.x or 3.x;koa@1.x
- For , use koa-xml-body@1.x.
- encoding: requested encoding. Default is utf8. If not set, the lib will retrive it from content-type(such as content-type:application/xml;charset=gb2312).1mb
- limit: limit of the body. If the body ends up being larger than this limit, a 413 error code is returned. Default is .content-length
- length: length of the body. When is found, it will be overwritten automatically.None
- onerror: error handler. Default is a . It means it will throws the error. You can config it to customize the response.{}
- xmlOptions: options which will be used to parse xml. Default is . See xml2js Options for details.body (ctx.request.body)
- key: A chance to redefine what the property name to use instead of the default .
`jscontent-type
app.use(xmlParser({
limit: 128,
encoding: 'utf8', // lib will detect it from ``
xmlOptions: {
explicitArray: false
},
key: 'xmlBody', // lib will check ctx.request.xmlBody & set parsed data to it.
onerror: (err, ctx) => {
console.error(err);
ctx.throw(err.status, err.message);
}
}))
