function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jason Adler 14Jason Adler 14 

How can I escape the double quotes and backslashes added in the string when making api call?

The below is the code in my callout class with keys left out. I have tested in postman and the JSON quote like: '{"User": {"account_expires" : "2022-11-18"}}' works perfectly to update when making the call. However, when I run from Salesforce using this callout class, in debug logs, I can see debug statement looks like: 
11:01:27.11 (14669480)|USER_DEBUG|[29]|DEBUG|{"User": {"account_expires" : "2022-11-18"}}
but it seems variable assignment actually looks lke:
11:01:27.11 (14553531)|VARIABLE_ASSIGNMENT|[27]|body|"{\"User\": {\"account_e (24 more) ..."
I am thinking I need to deseralize the JSON maybe by using another class but am having trouble with this. At the moment, the response I am receiving is:
11:01:28.153 (1172417713)|CALLOUT_RESPONSE|[34]|System.HttpResponse[Status=Not Found, StatusCode=404]
11:01:28.153 (1172459328)|HEAP_ALLOCATE|[34]|Bytes:90
11:01:28.153 (1172492874)|VARIABLE_SCOPE_BEGIN|[34]|response|System.HttpResponse|true|false
11:01:28.153 (1172567949)|VARIABLE_ASSIGNMENT|[34]|response|"System.HttpResponse[Status=Not Found, StatusCode=404]"|0x733b55dd

Does anyone have an idea to help me edit the below code so that I can receive the successful response?

Thanks!
 
public class LearnUponCalloutClass {
    	
    	 @future(callout=true)
   		 public static void makeCallout(Integer UserId) {
            
            //declare variables 
		String username = 'XYZ';
    		String password = 'XYZ';
    		
            //prepare request 
                HTTP h = new HTTP();
                HTTPRequest r = new HTTPRequest();
            
            //set endpoint and authorize 
                r.setEndpoint('https://dphie.learnupon.com/api/v1/users/' + UserId);
            
                Blob headerValue = Blob.valueOf(username + ':' + password);

		String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);

		r.setHeader('Authorization', authorizationHeader);
		r.setMethod('PUT');
          
                String body = '{"User": {"account_expires" : "2022-11-18"}}';
         
		System.debug(body);
            
                r.setBody(body);
             
              //send request
			HTTPResponse response = h.send(r);
      
}

 
Best Answer chosen by Jason Adler 14
Abdul KhatriAbdul Khatri
Hi Jason,

As per standard that status means 404 is a status code that tells a web user that a requested page is not available 

So to me it looks like there is an issue either with
As far the following response is concern, it means you are recieving json response which you are further serializing
11:01:27.11 (14553531)|VARIABLE_ASSIGNMENT|[27]|body|"{\"User\": {\"account_e (24 more) ..."

 

All Answers

Abdul KhatriAbdul Khatri
Hi Jason,

As per standard that status means 404 is a status code that tells a web user that a requested page is not available 

So to me it looks like there is an issue either with
As far the following response is concern, it means you are recieving json response which you are further serializing
11:01:27.11 (14553531)|VARIABLE_ASSIGNMENT|[27]|body|"{\"User\": {\"account_e (24 more) ..."

 
This was selected as the best answer
Jason Adler 14Jason Adler 14
Hi Abdul,

Thank you very much for the answer. You were correct that there was an issue with the header. I needed to add:
r.setHeader('Content-Type', 'application/json');
and now the request is working as expected with a successful response. I did not need to make any changes to the JSON body for serialization or deseralization, it was formatted correctly.

Jason