Skip to content

Commit c0c3053

Browse files
committed
Merge pull request #1 from mfahlandt/add-xmltype-as-parameter
add possibility to hand over custom content type as option
2 parents cc23b78 + e3999fa commit c0c3053

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ app.use(bodyParser.xml());
3535

3636
This will parse any XML-based request and place it as a JavaScript object on `req.body` for your route handlers to use.
3737

38-
An XML-based request is determined by the value of the `Content-Type` header. If this ends in `xml`, it will be parsed as XML. For example, the following Content-Types will all match:
38+
An XML-based request is determined by the value of the `Content-Type` header. If this ends in `/xml` or `/+xml`, it will be parsed as XML. For example, the following Content-Types will all match:
3939

4040
- `text/xml`
4141
- `application/xml`
4242
- `application/rss+xml`
4343

44+
But you can also set your own see Options:type
45+
4446
### Options
4547

4648
You can also pass in options:
@@ -63,6 +65,12 @@ When set to `true`, then deflated (compressed) bodies will be inflated; when `fa
6365

6466
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](https://www.npmjs.com/package/bytes) library for parsing. Defaults to `'100kb'`.
6567

68+
69+
#### type
70+
71+
set your own valid Content-Type that will be parsed, can be a string or an array, already valid topics are '*/xml', '+xml'
72+
73+
6674
#### verify
6775

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

index.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,36 @@
33
var xml2js = require('xml2js');
44

55
module.exports = function(bodyParser) {
6+
var xmlTypes = ['*/xml', '+xml'];
67
if(bodyParser.xml) {
78
// We already setup the XML parser.
89
// End early.
910
return;
1011
}
1112

12-
var xmlTypes = ['*/xml', '+xml'];
13-
1413
function xml(options) {
14+
var textParser;
1515
options = options || {};
16-
options.type = xmlTypes;
16+
if(options.type){
17+
if(Array.isArray(options.type)){
18+
xmlTypes = options.type.concat(xmlTypes);
19+
}else if(typeof options.type === 'string'){
20+
xmlTypes.push(options.type);
21+
}
22+
options.type = xmlTypes;
23+
}
1724

18-
var textParser = bodyParser.text(options);
25+
options.type = xmlTypes;
26+
textParser= bodyParser.text(options);
1927
return function xmlParser(req, res, next) {
28+
var parser;
2029
// First, run the body through the text parser.
2130
textParser(req, res, function(err) {
2231
if(err) { return next(err); }
2332
if(typeof req.body !== 'string') { return next(); }
2433

2534
// Then, parse as XML.
26-
var parser = new xml2js.Parser(options.xmlParseOptions);
35+
parser = new xml2js.Parser(options.xmlParseOptions);
2736
parser.parseString(req.body, function(err, xml) {
2837
if(err) {
2938
err.status = 400;

test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,32 @@ describe('XML Body Parser', function() {
6565
request(app)
6666
.post('/')
6767
.set('Content-Type', 'text/xml')
68-
.send('<CUSTOMER><name> Bob </name></CUSTOMER>')
68+
.send('<CUSTOMER><name>Bob</name></CUSTOMER>')
6969
.expect(200, { parsed: { customer: { name: 'Bob' } } }, done);
7070
});
7171

72+
it('should accept custom ContentType as array', function(done) {
73+
createServer({
74+
type: ['application/custom-xml-type']
75+
});
76+
request(app)
77+
.post('/')
78+
.set('Content-Type', 'application/custom-xml-type')
79+
.send('<customer><name>Bob</name></customer>')
80+
.expect(200, { parsed: { customer: { name: ['Bob'] } } }, done);
81+
});
82+
83+
it('should accept custom ContentType as string', function(done) {
84+
createServer({
85+
type: 'application/custom-xml-type'
86+
});
87+
request(app)
88+
.post('/')
89+
.set('Content-Type', 'application/custom-xml-type')
90+
.send('<customer><name>Bob</name></customer>')
91+
.expect(200, { parsed: { customer: { name: ['Bob'] } } }, done);
92+
});
93+
7294
it('should ignore non-XML', function(done) {
7395
createServer();
7496

0 commit comments

Comments
 (0)