0

我正在尝试在 JSON 请求中发送一个 sepcial/French 字符,例如:ÆÇÈ-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 请求的问题

4

1 回答 1

0

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

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

// 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);
        }
    }
});
于 2020-06-11T06:28:26.397 回答