I'm writing a code where I need to query the product object, get the product image URL, convert the Image in the URL to a base64 string for my other REST input. Here is the code that I'm using.

String url='https://myOrg.demo.my.salesforce.com/servlet/servlet.FileDownload?file=0152w000001Jex4';
HttpRequest req = new HttpRequest();
Http binding = new Http();
HttpResponse res = binding.send(req);
Blob image = res.getBodyAsBlob();
String encRes=EncodingUtil.base64Encode(image);

for (Integer i = 0; i < encRes.length(); i=i+300) {
    Integer iEffectiveEnd = (i+300 > (encRes.length()-1) ? encRes.length()-1 : i+300);

The output that I get is as below.

enter image description here

when I downloaded the same image and uploaded it online to get the base64 I get it as below. (cropped the bottom part of the output.).

enter image description here

I'm unable to understand why it is printing Li4U strings at the end.

Where am I going wrong in my code and how can I fix this.


  • Why not just query directly? Document d = [SELECT Body FROM Document WHERE Id = '0152w000001Jex4']; I'll see if I can replicate this, but it seems silly to burn a callout when a normal query would suffice.
    – sfdcfox
    Jan 18, 2021 at 16:42
  • Hi @sfdcfox, this fixed my issue mate. Thanks a lot :) Can you please add it to the solution? I'll accept it Jan 19, 2021 at 15:21

1 Answer 1


You don't need a callout for this, just query the content directly:

Document d = [SELECT Body FROM Document WHERE Id = '0152w000001Jex4'];

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.