@stead/fastify-xml-parser

1.0.0 • Public • Published

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"
}

Readme

Keywords

none

Package Sidebar

Install

npm i @stead/fastify-xml-parser

Weekly Downloads

17

Version

1.0.0

License

MIT

Unpacked Size

5.21 kB

Total Files

5

Last publish

Collaborators

  • mecolela