Registry / web-framework / body-parser-xml

body-parser-xml

JSON →
library2.0.5jsnpmunverified

`body-parser-xml` is an Express.js middleware that extends the widely used `body-parser` library, enabling applications to seamlessly parse incoming XML-formatted request bodies into JavaScript objects. Currently at stable version 2.0.5, this package integrates an `xml` method directly onto the `body-parser` object, allowing developers to handle XML APIs while working with familiar JSON-like structures. Its release cadence reflects active maintenance, with recent updates addressing Node.js version compatibility (supporting Node 10 and above) and critical security vulnerabilities, including prototype pollution and `xml2js` dependency issues. A key differentiator is its straightforward integration model, piggybacking on `body-parser`'s established middleware pattern and providing extensive options for XML parsing via the underlying `xml2js` library, such as `normalize` and `explicitArray`. This approach simplifies the handling of diverse XML content types in Express applications.

npm install body-parser-xml
INSTALL
IMPORT
SIG · BODY-PARSER-XML
B
body-parser-xml
web-frameworkjavascriptv2.0.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

body-parser-xml initialization (CommonJS)
const bodyParser = require('body-parser'); require('body-parser-xml')(bodyParser);
const bodyParserXml = require('body-parser-xml');
This module exports a function that must be called with an initialized `body-parser` object. This mutates the `body-parser` instance to add the `.xml()` method.
body-parser-xml initialization (ESM)
import bodyParser from 'body-parser'; import bodyParserXml from 'body-parser-xml'; bodyParserXml(bodyParser);
import { xml } from 'body-parser-xml';
For ESM environments, import both `body-parser` and `body-parser-xml`, then call `bodyParserXml` with the `bodyParser` instance to extend it.
bodyParser.xml middleware
app.use(bodyParser.xml({ limit: '1MB' }));
app.use(require('body-parser-xml'));
The `xml` method becomes available on the `bodyParser` object *after* the `body-parser-xml` module has been initialized. It's then used like any other `body-parser` middleware.

This quickstart initializes an Express server with `body-parser-xml` to parse incoming XML requests into JavaScript objects available on `req.body`. It demonstrates configuring the middleware with `limit` and `xmlParseOptions` and includes a sample route to process and respond to XML data.

const express = require('express'); const bodyParser = require('body-parser'); // Initialize body-parser-xml to extend body-parser require('body-parser-xml')(bodyParser); const app = express(); // Use the XML middleware // It parses application/xml, text/xml, and +xml content types by default app.use( bodyParser.xml({ limit: '1MB', // Reject payload bigger than 1 MB xmlParseOptions: { normalize: true, // Trim whitespace inside text nodes explicitArray: false, // Prevents elements with a single child from being an array }, }) ); // Define a route to handle XML POST requests app.post('/xml-data', (req, res) => { if (!req.body) { return res.status(400).send('No XML body received.'); } console.log('Received XML data:', JSON.stringify(req.body, null, 2)); res.json({ message: 'XML data received and parsed', data: req.body }); }); // Start the server const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); console.log('Send a POST request with Content-Type: application/xml to http://localhost:3000/xml-data'); console.log('Example cURL:'); console.log(`curl -X POST -H "Content-Type: application/xml" -d '<root><item>Hello</item><value>123</value></root>' http://localhost:3000/xml-data`); });
Debug
Known issues
breakingVersion 2.0.0 removed official support for Node.js versions older than 10. Users on older Node.js environments must upgrade their runtime or remain on `body-parser-xml` v1.x.
fix
Upgrade Node.js to version 10 or newer, or stick to `body-parser-xml` v1.x for legacy Node.js environments.
affects: >=2.0.0
breakingA prototype pollution vulnerability (CVE-2022-25927) was present in versions prior to 2.0.3, allowing attackers to inject arbitrary properties into JavaScript object prototypes. This could lead to various security risks, including remote code execution or denial of service.
fix
Upgrade to `body-parser-xml` version 2.0.3 or higher immediately to patch the prototype pollution vulnerability.
affects: <2.0.3
breakingA reported vulnerability in the `xml2js` dependency (issue #663) affected `body-parser-xml` versions prior to 2.0.4. While specific details might vary, it indicates potential risks related to XML parsing.
fix
Upgrade to `body-parser-xml` version 2.0.4 or higher to benefit from the updated `xml2js` dependency and associated security fixes.
affects: <2.0.4
gotchaUnlike typical middleware that are directly imported and used, `body-parser-xml` functions as an enhancer. You must pass an initialized `body-parser` object to the `body-parser-xml` module to add the `.xml()` method, rather than directly using `body-parser-xml` as middleware.
fix
Ensure you call `require('body-parser-xml')(bodyParser);` (or its ESM equivalent) *before* attempting to use `bodyParser.xml()` in your Express application.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: bodyParser.xml is not a function
The `body-parser` object was not correctly extended by `body-parser-xml`.
fix
Ensure you have called `require('body-parser-xml')(bodyParser);` (or `bodyParserXml(bodyParser);` in ESM) after requiring `body-parser`.
Error: request entity too large
The incoming XML request body exceeded the configured `limit` option.
fix
Increase the `limit` option in `bodyParser.xml({ limit: '5MB' })` or adjust the client payload size. Default limit is '100kb'.
XML parse error: Non-whitespace characters not allowed in prolog
The incoming request body is not valid XML, or the `Content-Type` header is incorrect, causing the parser to attempt to parse non-XML data as XML.
fix
Verify that the client is sending well-formed XML and that the `Content-Type` header (e.g., `application/xml`, `text/xml`) is set correctly. If using a custom type, configure the `type` option in `bodyParser.xml({ type: 'application/x-my-xml' })`.
Upgrade
Version history
2.0.5latest on npm
Audit
Dependencies
body-parserrequiredRequired as a peer dependency, `body-parser-xml` extends its functionality by adding an `xml` method.
expressrequiredRequired for `app.use()` to register the XML parsing middleware.
xml2jsrequiredInternal dependency for XML parsing, its options are exposed via `xmlParseOptions`.
Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources
body-parser-xml — npm install body-parser-xml · libregistry