I am trying to parse the response body which is in "base64 content of a file" and trying to create the attachment with the body but getting error of " Unrecognized base64 character: \" I have included sample code of the response


When i am trying to use this response body https://www.freeformatter.com/base64-encoder.html then i am able to download the pdf but getting error when trying to decode it from Sales force

Here is the code which i have written

List<Object> filesList = (List<Object>)responseMap.get('files');
                Map<String, Object> files = (Map<String, Object>)filesList.get(0);
                //String fileId = (String)files.get('id');
                string fileId='92080882-cc3c-407d-9552-63ee02064733';
                Http http = new Http();
                HttpRequest request = new HttpRequest();
                request.setheader('Content-Type', 'application/json; charset=UTF-8');
                request.setHeader('Accept', 'application/json');
                request.setHeader('Authorization','Bearer '+key);
                System.debug(' Procedure Creation '+request.getBody());
                System.debug(' Procedure Creation '+request.getEndpoint());
                System.debug(' Procedure Creation '+request.getHeader('Authorization'));
                HttpResponse response1 = http.send(request);
                if (response1.getStatusCode() == 200) {
                    String responseBody = response1.getbody();
string responsestring=responsebody.remove('\"');
                    system.debug('Response body---->'+responseBody);
                    Attachment att = new Attachment();
                    att.ParentId = yp.ID_parent_attachment__c;
                    att.Name = yp.Name+' Attachment.pdf';
                    blob body=EncodingUtil.base64Decode(responsestring);

                    att.Body = body;

                    insert att;
                    System.debug('The status code returned was not expected: ' +
                                 response1.getStatusCode() + ' ' + response1.getStatus());
            System.debug('#### Start Procedure Error Message => '+response.ErrorMessage);

I have reviewed many articles regarding this but unable to get any result


The content you are receiving is not Base64: it is Base64 inside JSON, which you are asking the server to provide to you. Base64 encoding uses the characters [A-Za-z0-9+/]. The backslash and quote characters shown here are not part of the Base64 encoding but the JSON wrapper around it.

You need to deserialize the JSON first, into a String, and then Base64-decode that String:

String base64Content = ( String ) JSON.deserialize( response.getBody(), String.class );
Blob body = EncodingUtil.base64Decode(base64Content);

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.