You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constkoa=require('koa')constxmlParser=require('koa-xml-body')constapp=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 undefinedctx.body=ctx.request.bodyreturnnext()})
koa-xml-body will carefully check and set context.request.body, so it can intergate well with other body parsers such as koa-bodyparser:
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).
limit: limit of the body. If the body ends up being larger than this limit, a 413 error code is returned. Default is 1mb.
length: length of the body. When content-length is found, it will be overwritten automatically.
onerror: error handler. Default is a None. 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.
key: A chance to redefine what the property name to use instead of the default body (ctx.request.body).
app.use(xmlParser({limit: 128,encoding: 'utf8',// lib will detect it from `content-type`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);}}))