如何使用 datapower 中的 xslt 将特殊/法语字符从 JSON 绕过到 XML



我正在尝试在 JSON 请求中发送一个特殊/法语字符,例如:ÆÇÈ-1 Çâfé's Çôrp-Ltd,但它失败并出现以下错误:

{ "error":{ "code":"4000", "message":"Invalid Request From Consumer. Error Description :0x00c30025 Unable to parse JSON and generate JSONx: illegal character 's' at offset 8951

然而,当我尝试将相同的名称 -ÆÇÈ-1 Çâfé 的 Çôrp-Ltd作为 XML 请求传递时,它正在通过成功响应进行处理。

请帮助我解决 JSON 请求的问题

首先要检查的是您的服务(我假设的MPGW(是否设置为Request data: JSON而不是XML。 如果JSON为请求数据不起作用,请尝试使用non-XML. DataPower 根据内容数据类型运行不同的解析器/验证器,但只要 JSON 本身有效JSON应该可以工作,否则打开 PMR!

如果您仍然无法弄清楚,请测试传入的数据是否被检测为 UTF-8 而不是 Latin1。添加一个 GWS 操作并将输入输入提供给它,然后尝试如下操作:

// This is where we start, grab the INPUT as a buffer
session.input.readAsBuffers(function(readAsBuffersError, data) {
if (readAsBuffersError) {
console.error('Error on readAsBuffers:', readAsBuffersError);
} else {
let content = data.toString();
if (content.length === 0) {
console.error('Empty message found for Request message!');
} else {
try {
// This seems like an overkill solution but we need to know if data fetched is UTF-8 or Latin1
// If Latin1 we need to wobble it around to not wreck "double-bytes", e.g. Å, Ä or Ö
const util = require('util');
const td = new util.TextDecoder('utf8', { fatal: true });
td.decode(Buffer.from(content));
console.log('Buffer data is UTF-8!');
} catch (err) {
// It is not UTF-8, since it failed so we'll assume it is Latin1. If not it will throw it's own Buffer error or transformation will fail...
if (err.message === 'The encoded data was not valid for encoding utf-8') {
const l1Buffer = Buffer.from(content, 'latin1');
const l1String = l1Buffer.toString('latin1');
content = Buffer.from(l1String, 'utf8');
console.log('Buffer data was Latin1, now converted into UTF-8 successfully!');
}
}
session.output.write(content);
}
}
});

相关内容

  • 没有找到相关文章

最新更新