0

I'm trying to send a sepcial/French character like : ÆÇÈ-1 Çâfé's Çôrp-Ltd in the JSON request, but it is failing with the below error:

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

whereas, when I try to pass the same name- ÆÇÈ-1 Çâfé's Çôrp-Ltd as a XML request, it is getting processed with success response.

Please help me resolve the issue with the JSON request

potame
  • 7,597
  • 4
  • 26
  • 33
sush
  • 1
  • 1
  • As per https://jsonlint.com/ this is a valid JSON string. Could you please provide a sample of where this appears in a JSON request so that we can see the context of the error? – Tom W Jun 09 '20 at 11:59
  • Thanks Tom, Yes it says it is a valid JSON.... but when we send that request as below for the XSLT, it throws the error as mentioned above- Unable to parse JSON and generate JSONx: illegal character PFB the sample JSON request: "new_account_employer_details": { "name": "ÆÇÈ-1 Çâfé's Çôrp-Ltd", "street1": "APT666", "street2": "189-34 Great George Street","state": "PE", "mail_code": "C0A1C0", "country": "124" }, – sush Jun 09 '20 at 12:33

1 Answers1

0

First thing to check is that your service (a MPGW I assume) is set to Request data: JSON and not XML. If JSON as request data isn't working either try with non-XML. DataPower runs different parsers/validators depending on the content data type but as long as the JSON itself is valid JSON should work, else open a PMR!

If you still can't figure it out test that the incoming data is detected as UTF-8 and not as Latin1. Add a GWS action and feed the INPUT to it and try something like:

// 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);
        }
    }
});
Anders
  • 3,198
  • 1
  • 20
  • 43