Fix: XML body-parser in testbench server is giving error

This commit is contained in:
ramki-bruno
2025-05-20 00:19:20 +05:30
parent dee6acbf09
commit 427025fc2b
2 changed files with 9 additions and 14 deletions

View File

@@ -29,8 +29,8 @@ const saveRawBody = (req, res, buf) => {
app.use(bodyParser.json({ verify: saveRawBody }));
app.use(bodyParser.urlencoded({ extended: true, verify: saveRawBody }));
app.use(bodyParser.text({ verify: saveRawBody }));
app.use(xmlParser());
app.use(express.raw({ type: '*/*', limit: '100mb', verify: saveRawBody }));
app.use(xmlParser());
formDataParser.init(app, express);

View File

@@ -4,23 +4,18 @@ const xmlParser = () => {
const parser = new XMLParser({
ignoreAttributes: false,
allowBooleanAttributes: true,
attributeNamePrefix: '',
isArray: (name, jpath, isLeafNode) => isLeafNode,
});
return (req, res, next) => {
if (req.is('application/xml') || req.is('text/xml')) {
let data = '';
req.setEncoding('utf8');
req.on('data', (chunk) => {
data += chunk;
});
req.on('end', () => {
try {
req.body = parser.parse(data);
next();
} catch (err) {
res.status(400).send('Invalid XML');
}
});
try {
req.body = parser.parse(req.rawBody);
next();
} catch (err) {
res.status(400).send('Invalid XML');
}
} else {
next();
}