0

我正在寻找现有正文解析器的扩展。我需要使用 loopback4 节点解析 xml 数据。我尝试将 content-type 设置为 application/xml,但它给了我一个不受支持的内容类型错误。不支持xml。我在 loopback4 文档中没有找到任何示例。

我试过下面的代码:

@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;
    }
  }

你能帮忙吗。。谢谢。

4

1 回答 1

0

你有任何进展吗?

作为解决方法,我使用 text/plain 代替 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);
    }

  }
于 2021-04-13T19:12:26.297 回答