koa-xml-body is a Koa middleware designed to parse XML request bodies, making the parsed data available on `ctx.request.body`. The current stable version is 3.0.0, published approximately 3 years ago, which is compatible with Koa 2.x and 3.x, while Koa 1.x users should opt for `koa-xml-body@1.x`. It internally uses `xml2js` for the actual XML parsing, exposing its configuration options via the `xmlOptions` property. Key differentiators include its explicit focus on XML content types (e.g., `application/xml`, `text/xml`), robust error handling customization through an `onerror` callback, and seamless integration with other body parsers by carefully managing `ctx.request.body`. Its release cadence is generally tied to bug fixes or Koa compatibility updates rather than rapid feature additions, suggesting a maintenance-oriented status.
npm install koa-xml-bodyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up a Koa server with `koa-xml-body` to parse incoming XML requests. It includes custom options for body size limit, XML parsing behavior (e.g., `explicitArray`, `ignoreAttrs`), a custom request key, and an error handler for invalid XML, making it robust for production use.
Upgrade Koa to version 2.x or 3.x, or explicitly install `koa-xml-body@1.x` if tied to Koa 1.x.
Always provide a custom `onerror` function in the middleware options to gracefully handle parsing errors, e.g., `onerror: (err, ctx) => { ctx.status = 400; ctx.body = 'Bad Request: Invalid XML'; }`.Configure the `limit` option in the middleware to a higher value if needed, e.g., `app.use(xmlParser({ limit: '5mb' }))`. Ensure your server resources can handle the increased payload size.Place `koa-xml-body` before other generic body parsers if it's meant to be the primary handler for XML. The library is designed to co-exist, so ensure the content-type allows `koa-xml-body` to process first.
Increase the `limit` option when initializing the middleware: `app.use(xmlParser({ limit: '5mb' }))` to accommodate larger XML payloads.Ensure the client sends well-formed XML without any leading non-whitespace characters. If the issue persists, review the `content-type` header to confirm it is `application/xml` or `text/xml`.
Verify that the request's `Content-Type` header is set to `application/xml` or `text/xml`. Check for preceding body parsing middleware that might have already consumed or processed the request stream. Add a `console.log(ctx.request.body)` to debug when it becomes `undefined`.