Giter VIP home page Giter VIP logo

body-parser-xml's Introduction

XML Body Parser

Adds XML parsing to the body-parser library, so you can convert incoming XML data into a JSON representation.

This is really useful if you want to deal with plain old JavaScript objects, but you need to interface with XML APIs.

Build Status npm version Dependency Status devDependency Status

Installation

npm install --save express body-parser body-parser-xml

Usage

This library adds an xml method to the body-parser object.

Initialise like so:

var bodyParser = require('body-parser');
require('body-parser-xml')(bodyParser);

Once initialised, you can use it just like any other body-parser middleware:

var app = require('express')();
app.use(bodyParser.xml());

This will parse any XML-based request and place it as a JavaScript object on 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 or +xml will be parsed as XML. For example, the following Content-Types will all match:

  • text/xml
  • application/xml
  • application/rss+xml

If you need to match against a custom Content-Type header, pass in the type to match as an option (see below).

Options

You can also pass in options:

app.use(bodyParser.xml(options));

The options object accepts any of the following keys:

defaultCharset

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 utf-8.

inflate

When set to true, then deflated (compressed) bodies will be inflated; when false, deflated bodies are rejected. Defaults to true.

limit

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 '100kb'.

type

The expected Content-Type of the XML request to be parsed. Overrides the default content types, can be a String or Array of Strings.

verify

The 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.

xmlParseOptions

This option controls the behaviour of the XML parser. You can pass any option that is supported by the xml2js library: see here for a list of these options.

Example

var express = require('express'),
    bodyParser = require('body-parser');

require('body-parser-xml')(bodyParser);

var app = express();
app.use(bodyParser.xml({
  limit: '1MB',   // Reject payload bigger than 1 MB
  xmlParseOptions: {
    normalize: true,     // Trim whitespace inside text nodes
    normalizeTags: true, // Transform tags to lowercase
    explicitArray: false // Only put nodes in array if >1
  }
}));

app.post('/users', function(req, res, body) {
  // Any request with an XML payload will be parsed
  // and a JavaScript object produced on req.body
  // corresponding to the request payload.
  console.log(req.body);
  res.status(200).end();
});

Motivation

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.

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.