Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining XML in the Request Body for Post in Nest.js

I am curious if it is possible to obtain XML data in the Request Body of Nest.js.

Dependencies

"dependencies": {
    "@nestjs/common": "^7.0.0",
    "@nestjs/core": "^7.0.0",
    "@nestjs/platform-express": "^7.0.0",

Requirement

I wish to have an HTTP POST API called /EPCIS/capture that would obtain XML documents like the following:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<epcis:EPCISDocument
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:epcis="urn:epcglobal:epcis:xsd:1"
    xmlns:epcglobal="urn:epcglobal:xsd:1"
    xsi:schemaLocation="urn:epcglobal:epcis:xsd:1 EPCglobal-epcis-1_0.xsd"
    creationDate="2008-03-16T22:13:16.397+01:00"
    schemaVersion="1.0">
  <EPCISBody>
    <EventList>
      <ObjectEvent>
        <eventTime>2008-03-16T22:13:16.397+01:00</eventTime>
        <eventTimeZoneOffset>+01:00</eventTimeZoneOffset>
        <epcList>
          <epc>urn:epc:id:sgtin:0614141.107346.2017</epc>
          <epc>urn:epc:id:sgtin:0614141.107346.2018</epc>
        </epcList>
        <action>OBSERVE</action>
        <bizStep>urn:epcglobal:epcis:bizstep:fmcg:shipped</bizStep>
        <disposition>urn:epcglobal:epcis:disp:fmcg:unknown</disposition>
        <readPoint>
          <id>urn:epc:id:sgln:0614141.07346.1234</id>
        </readPoint>
        <bizLocation>
          <id>urn:epcglobal:fmcg:loc:0614141073467.A23-49</id>
        </bizLocation>
        <bizTransactionList>
          <bizTransaction type="urn:epcglobal:fmcg:btt:po">
            http://transaction.acme.com/po/12345678
          </bizTransaction>
        </bizTransactionList>
      </ObjectEvent>
    </EventList>
  </EPCISBody>
</epcis:EPCISDocument>

Within my Controller:


Post('capture')
    addEPCDocument(@Body() epcDocument: any): any {
        console.log(epcDocument)
    }

But all I get is {} when logging the incoming Request Body. My POSTMAN setting already mentions:

Content-Type: application/xml

and within the Body I have the above mentioned XML pasted. The Response is HTTP 400 Bad Request.

What is normally a way to extract XML from the Request Body in Nest.JS?

like image 229
Shan-Desai Avatar asked Apr 01 '20 15:04

Shan-Desai


2 Answers

Nest come with body-parser pre-defined, but you can modify the configurations it uses to work with xml. By default, it will only work with application/json and applicaiton/x-www-form-urlencoded. You can use a different middleware for parsing the xml requests, like this one

like image 60
Jay McDoniel Avatar answered Sep 16 '22 23:09

Jay McDoniel


As Jay mentioned, middleware for parsing the xml requests can be added.

// xml.middleware.ts

import { Injectable, NestMiddleware } from '@nestjs/common';
import * as bodyParser from 'body-parser';

const bodyParserXML = bodyParser.text({
  type: 'application/xml',
});

@Injectable()
export class XMLMiddleware implements NestMiddleware {
  use(req: any, res: any, next: () => void) {
    parserXML(req, res, next);
  }
}

Add this middleware in app.module.ts

import { XMLMiddleware } from './middileware/xml.middleware';
    
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(XMLMiddleware).forRoutes({
      path: '/*',
      method: RequestMethod.GET,
    });
  }
}

reference: https://chowdera.com/2022/117/202204271800482265.html

like image 41
iravinandan Avatar answered Sep 16 '22 23:09

iravinandan