2

We have a custom REST api that throws an error when ever we have a diacritic character as part of the JSON request. For example if the character "é" is anywhere in the JSON it errors when we convert to string.

Here is a snippet of the code:

    @HttpPost
    global static String fetcheRAMmsg()
        {
            RestRequest req = RestContext.request;

            Map<String,String> eramFields = new Map<String,String>();
            String ackId;
            String errorMsg='None';
            system.debug('%%% - About to Parse the JSON: ' );
            system.debug('%%% - Headers from eRAM: '+req.headers);
            system.debug('%%% - About to Convert to String' );
            system.debug('%%% - JSON from eRAM: '+req.requestBody.toString());
            JSONParser parser = JSON.createParser(req.requestBody.toString());

It fails at the .tostring() command.

It works fine if there are no diacritic charcters in it. How do I check convert the BLOB to a string?

2

Try this. You can't convert "BINARY" data into a String. A String only supports valid UTF-8 strings. Use EncodingUtil.Base64Encode if you need the binary data in a string you can work with.

String requestBodyResp = EncodingUtil.base64Encode(req.requestBody);
JSONParser parser = JSON.createParser(requestBodyResp);
| improve this answer | |
  • Why? Please add some explanation rather than just a code dump. – Adrian Larson Jun 5 '18 at 20:57
1

This code:

System.debug(Blob.valueOf('abcéééxyz').toString());

runs fine.

Suggest you examine the exact bytes that are sent to your end point.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.