3

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

"JVBERi0xLjQKJeLjz9MKNCAwIG9iaiA8PC9GaWx0ZXIvRmxhdGVEZWNvZGUvTGVuZ3RoIDUzOTg+PnN0cmVhbQp4nM1dzXLbuLLe+ymwueWkyuGIFCVRs9M4mjk6Zcsex557qyZnQZOUzBRFyiCp69zHyJPMI3kZzuI8wwVIQKQ+AKIyk8WppKpBoPvrrxvNHxEk\/Xz20\/3ZgEymDrkPz+b3Z7+ePZ8NrMFwMiL\/e+aQf7LBT2f2gFyf\/f6vAQnPhmMyGY3J5mw0aVpJ3RqOrYHL2sODZjP+dPbfZynD4f\/omjv84Web2J7FfNyvzux6wCbexHJHZDJ0uLjfnL25vFne383uyfs5YfK32eKKzMj7h7v5nHXdz++uF8v5\/O39J07bBOo4U2s8JRN7YE28GvV+fn37gdzO7u4X8yth7TpkzNw7PC7bHVueLbeTdpuJEduudbttofd0tpIsPMs5ICHUxpOawJxFNSdX8w\/kw83Dhw+LX5as+eNhHDqEiWu5NcCVT\/IsiKuiIvPrJVlGRZF99tdRF8LRQoxHlt2QuIpIHn9dRzWSn5AoL1hHTt6RfpTRyJo2M7TYbPyCxkGZVBHxS3JnXVofLIZZ5iSJy.....

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.setEndpoint(youSignSetting[0].Base_URL__c+'/files/'+fileId+'/download');
                request.setMethod('GET');
                request.setheader('Content-Type', 'application/json; charset=UTF-8');
                request.setHeader('Accept', 'application/json');
                request.setHeader('Authorization','Bearer '+key);
                request.setTimeOut(120000);
                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();
                    //Encodingbody=EncodingUtil.base64Decode(responseBody);
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;
                }
                else{
                    System.debug('The status code returned was not expected: ' +
                                 response1.getStatusCode() + ' ' + response1.getStatus());
                    System.debug(response1.getBody());
                }
            }
        }else{
            System.debug('#### Start Procedure Error Message => '+response.ErrorMessage);
        }
    }
}

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

1 Answer 1

10

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);
0

You must log in to answer this question.

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