1

I'm looking for an extension to existing body parsers.. I have a requirement to parse xml data using loopback4 node.. I have tried setting content-type to application/xml but it is giving me an unsupported content type error. xml is not supported. I did not find any examples in loopback4 documentation.

I have tried below code:

@post('/dashboard/parseXmlRequest/', {
    responses: {
      '200': {
        description: 'parses the xml and sends the response',
        content: {
          'application/xml': {
            schema: {
              type: 'any'
            }
          }
        },
      },
    },
  })
  async parseXmlRequest(
    @requestBody({
      content: {
        'application/xml': {
          schema: {type: 'object'},
        },
      },
    })
    data: object) {
    try {
      console.log("request : ", data);
      return await this.myService.deviceResponse(body);
    }
    catch (err) {
      console.log(err);
      return err;
    }
  }

Can you please help.. thank you.

Rifa Achrinza
  • 1,555
  • 9
  • 19
Ravi
  • 11
  • 2
  • **Welcome to Stackoverflow**, `to maximise your chance of getting an answer`, please **[take the tour](https://stackoverflow.com/tour)** and read **[How do I ask a good question](https://stackoverflow.com/help/how-to-ask)**. `Update your question` with just enough code to allow others to reproduce the problem. For help with this, read **[How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/minimal-reproducible-example)**. – amarinediary Dec 25 '20 at 05:58

1 Answers1

0

have you made any progress?

As workaround, I'm using text/plain instead xml;

@post('/data/import', {
    responses: {
      '200': {
        description: 'Import XML',
        content: {'application/json': {type: 'any' }},
      },
    },
  })
  async import(
    @requestBody({
      content: {
        'text/plain': {
        },
      },
    })
    xml : string,
  ): Promise<string> {
    return this.dataRepository.import(xml);
  }

async import( xml: string) : Promise<string> {

    try {
      console.log("XML =>",xml);
      const result = await xml2js.parseStringPromise(xml, {mergeAttrs: true});
      const json = JSON.stringify(result, null, 4);
      return json;
    } catch (err) {
      console.log(err);
      return Promise.resolve(err);
    }

  }
fhansen
  • 43
  • 6