XML Body Parser
Adds XML parsing to the body-parser library, so you can convert incoming XML data into a JSON representation.
npm install --save express body-parser body-parser-xml
This library adds an
xml method to the
Initialise like so:
var bodyParser = require('body-parser'); require('body-parser-xml')(bodyParser);
Once initialised, you can use it just like any other
var app = require('express')(); app.use(bodyParser.xml());
req.body for your route handlers to use.
An XML-based request is determined by the value of the
Content-Type header. By default, any
Content-Type header ending in
+xml will be parsed as XML. For example, the following Content-Types will all match:
If you need to match against a custom
Content-Type header, pass in the
type to match as an option (see below).
You can also pass in options:
options object accepts any of the following keys:
Specify the default character set for the text content if the charset is not specified in the
Content-Type header of the request. Defaults to
When set to
true, then deflated (compressed) bodies will be inflated; when
false, deflated bodies are rejected. Defaults to
Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the bytes library for parsing. Defaults to
Content-Type of the XML request to be parsed. Overrides the default content types, can be a String or Array of Strings.
verify option, if supplied, is called as
verify(req, res, buf, encoding), where
buf is a
Buffer of the raw request body and
encoding is the encoding of the request. The parsing can be aborted by throwing an error.
This library was born out of a frustration that express-xml-bodyparser, the most popular XML-parsing library for express, doesn't support the regular
body-parser options - in particular, limiting the payload size.
This library was written to use
body-parser's text parser under the hood, and then passes the parsed string into the XML parser. We can therefore take advantage of
body-parser's regular options, and support limiting the payload size, amongst other things.