我正在寻找现有正文解析器的扩展..我需要使用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;
}
}你能帮帮我吗?谢谢。
发布于 2021-04-14 03:12:26
你有什么进展吗?
作为变通方法,我使用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);
}
}https://stackoverflow.com/questions/65439085
复制相似问题