0

How to remove the escape characters"\" from the Response and the the double quotes at the beginning and the end from the Response ? please check the expected valid response in the below. Code :

@HttpPost
global static string dopost() {                
          RestRequest req = RestContext.request;
          RestResponse res = RestContext.response;
          Case tkt= (Case)JSON.deserialize(req.requestBody.toString(),Case.class);
          system.debug(tkt);
          try {              
            insert tkt;
          }
          catch(DmlException e) {
         return 'The following exception has occurred: ' + e.getMessage();
    }      
         Case insertedCase = [Select Id,CaseNumber,Subject,Status,Reason,Origin from Case Where Id =:tkt.Id];       
             return JSON.serialize(insertedCase);         
}

Response : "{\"attributes\":{\"type\":\"Case\",\"url\":\"/services/data/v37.0/sobjects/Case/5002800000SzkMOAAZ\"},\"Id\":\"5002800000SzkMOAAZ\",\"CaseNumber\":\"00001081\",\"Subject\":\"TWX-Request\",\"Status\":\"New\",\"Reason\":\"Deviation\",\"Origin\":\"TWX\"}"

Valid Response to parse : {"attributes":{"type":"Case","url":"/services/data/v37.0/sobjects/Case/5002800000SzkMOAAZ"},"Id":"5002800000SzkMOAAZ","CaseNumber":"00001081","Subject":"TWX-Request","Status":"New","Reason":"Deviation","Origin":"TWX"}

  • Where are you consuming this service, in or out of Salesforce – Raul Oct 13 '16 at 8:24
  • Out of Salesforce @Rahul Sharma – B1 sfdc Oct 13 '16 at 8:29
1

The problem is you are not setting proper Content-Type response headers. In your response to Ashwani, you stated that you added res.addHeader(), but that line will not make much sense, if you are not returning the RestContext.response, as is clearly the case judging from your code. What you are returning is simply a string created by JSON.serialize() function. Would not make much of a difference if you returned any literal string for that matter. So whatever you return as response that way is ignoring the context of the RestContext.response.

What I would suggest is that you modify your HttpPost method, firstly by changing the return type from string to void. And secondly by not returning any explicit values, but instead setting the values you wanted to return into the RestContext response body.

E.g.

@HttpPost
global static string doPost() {                
    RestRequest req = RestContext.request;
    RestResponse res = RestContext.response;

    Case tkt = (Case)JSON.deserialize(req.requestBody.toString(),Case.class);

    try {              
        insert tkt;

        Case insertedCase = [Select Id,CaseNumber,Subject,Status,Reason,Origin from Case Where Id =:tkt.Id];       

        //return JSON.serialize(insertedCase); 
        res.addHeader('Content-Type', 'application/json');
        res.responseBody = Blob.valueOf(JSON.serializePretty(insertedCase));
        res.statusCode = 200;  
    }
    catch(DmlException e) {

        //return 'The following exception has occurred: ' + e.getMessage();
        res.addHeader('Content-Type', 'application/json');
        res.responseBody = Blob.valueOf('{ "errorMessage" : "' +e.getMessage()+'" }');
        res.statusCode = 400;
    }
}

Hope it helps!

0

This is a common issue. Few things you can do to avoid this:

  1. When you send request to this web-service, make sure you set content-type header as application/json. And vice-verse using the RestResponse class variable headers.

  2. If above solution doesn't work. Find the similar method in external requester which can convert string to json or remove escape slashes and then parse that json string to relevant object.

  • I have added res.addHeader('Content-Type', 'application/json;charset=UTF-8'); //Content-Type: application/json;charset=UTF-8 – B1 sfdc Oct 13 '16 at 9:27
  • But of no use... Still am getting Raw Response as : "{\"attributes\":{\"type\":\"Case\",\"url\":\"/services/data/v37.0/sobjects/Case/5002800000SztelAAB\"},\"Id\":\"5002800000SztelAAB\",\"CaseNumber\":\"00001090\",\"Subject\":\"First NNTest\",\"Status\":\"New\",\"Reason\":\"Deviation\",\"Origin\":\"Web\"}" – B1 sfdc Oct 13 '16 at 9:30
  • How do we do it for a LIST of Cases and Responses. – B1 sfdc Nov 3 '16 at 9:30

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.