@stead/fastify-xml-parser
v1.0.0
Published
Fastify plugin to parse XML payload into JS object
Downloads
67
Readme
Fastify XML Parser
Fastify Plugin
Fastify plugin to parse XML payloads into JSON objects.
A custom fork of fastify-xml-body-parser for fastify v3
Installation
yarn add @stead/fastify-xml-parser
Usage
This plugin use fast-xml-parser to parse the XML payload. So it accepts all the options supported by fast-xml-parser.
import { fastify } from 'fastify';
import { XMLParser } from '@stead/fastify-xml-parser';
const options = {
attributeNamePrefix : "@_",
attrNodeName: "attr", //default is 'false'
textNodeName : "#text",
ignoreAttributes : true,
ignoreNameSpace : false,
allowBooleanAttributes : false,
parseNodeValue : true,
parseAttributeValue : false,
trimValues: true,
decodeHTMLchar: false,
cdataTagName: "__cdata", //default is 'false'
cdataPositionChar: "\\c",
validate: false,
contentType: ['application/xml', 'text/xml']
};
fastify.register(XMLParser, options);
fastify.post('/', (req, res) => {
res.send(Object.assign({}, req.body))
});
fastify.listen(8000, (err) => {
if (err) throw err
});
Sample POST body / payload
<sample>
data
</sample>
The resulting data would be an object:
{
"sample": "data"
}