[SalesForce] Salesforce REST JSON not getting serialized properly

Please help me to get this JSON string deserialize as map of list of string.

public static void postPermissionSetAutoDelegation(Map<string,Set<String>> delegationMap,String OrgName)
{
    String jsonstr = (String)JSON.serialize(delegationMap);

    //The JSonStr ->   {"ITL8090":["Administrator:TRUE","Bank_Operations_Reviewer:TRUE"]} 
    //Code skipped Retriving accessToken and instance URL

    String uri = ResponseMap.get('instance_url') + '/services/apexrest/PermSetDelegation/';
    System.debug(uri);
    //Post the data         
    HttpResponse res = send(uri,'HttpPost',jsonstr,ResponseMap.get('access_token')); //End point should be rest service URL
    System.debug('***'+res);
}

My Post method in another org

@HttpPost  
global static  Map<String, List<String>> processPermSetDelegation()
{
    RestRequest req = RestContext.request;
    Map<String, List<String>> delegationMap= (Map<String, List<String>>) JSON.deserializeUntyped(String.valueOf(req));  //Error         
    system.debug(req);
    return delegationMap;
    //Parse and send the data to handler class
} 

Error Message:
RESPONSE_BODY[{"errorCode":"APEX_ERROR","message":"System.JSONException:
Unexpected character ('R' (code 82)): expected a valid value (number,
String, array, object, 'true', 'false' or 'null') at input location
[1,2]\n\nClass.System.JSON.deserializeUntyped: line 11, column
1\nClass.Method processPermSetDelegation:
line 9, column 1"}]

Best Answer

You are currently trying to deserialize the RestRequst SObject to a Map. Instead, you want to deserialize the RestRequest's body, which contains the POSTed data. Here is the API page for the RestRequest class:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_restrequest.htm#apex_methods_system_restrequest.

Using the request body, your code would then look something like this:

@HttpPost  
global static  Map<String, List<String>> processPermSetDelegation()
{
    RestRequest req = RestContext.request;
    String requestBody = req.requestBody.toString();
    System.debug(requestBody);
    Map<String, List<String>> delegationMap = (Map<String, List<String>>) JSON.deserializeUntyped(requestBody);        
    System.debug(delegationMap);
    return delegationMap;
    //Parse and send the data to handler class
}

However, I advise posting params and then retrieving them via the RestRequest.params property.