I have a question regarding this post: handle PDF content in HTTP callout

When I try the above code I'm getting this:

"Unrecognized base64 character: %"

The logs show this as well:

19:53:04:658 VARIABLE_ASSIGNMENT [285]|base64Response|"%PDF-1.3\r\n%����\r\n3 0 (82794 more) ..."

Any ideas?


  • That's not a base-64 string, it's the actual binary PDF data.
    – sfdcfox
    Nov 13, 2019 at 15:26

1 Answer 1


You are getting a blob directly. Why not save the blob directly using resp.getBodyAsBlob()

String endPoint ='http://resources.docs.salesforce.com/rel1/doc/en-us/static/pdf/salesforce_query_search_optimization_developer_cheatsheet.pdf';

HTTP h = new HTTP();
HTTPRequest request = new HTTPRequest();
request.setHeader('contentType', 'application/json');
HTTPResponse resp = h.send(request);

    Attachment attach = new Attachment();
    attach.contentType = 'application/octet-stream';
    attach.name = 'myfile111.pdf';
    attach.parentId = '0010C000006ReQW';
    attach.body = resp.getBodyAsBlob();
    insert attach;

In my code, I am downloading a SF document PDF and saving it as an attachment on an account record

  • That did the trick! Thanks so much!
    – George
    Nov 15, 2019 at 4:42
  • @George can you accept answer? It will help someone with similar querry reach solution faster Nov 15, 2019 at 6:05

You must log in to answer this question.

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