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 Answers 2

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);
1
  • Why? Please add some explanation rather than just a code dump.
    – Adrian Larson
    Commented Jun 5, 2018 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .