I have to get a pdf from an external web service and store it as an attachment of an object.

Im getting the pdf with the following code

public override Blob getPDFReport(String token, String jobId) {
    HttpRequest req = new HttpRequest();
    req.setEndpoint(DOMAIN + PDF_REPORT_SERVICE + jobId);
    req.setHeader(HEAD_CONNTENT, 'application/pdf');
    //req.setHeader(HEAD_ACCEPT, JSON_TYPE);
    //req.setHeader(HEAD_VERSION, API_1_16_6);
    String tokenHeadValue = TOKEN_AUTH.replace(REPLACE_TOKENID, token);
    req.setHeader(HEAD_AUTH, tokenHeadValue);
    HTTPResponse res = this.sendRequest(req);
    for(String key : res.getHeaderKeys()) {
        System.debug(key + ' ->' + res.getHeader(key));
    Blob body = res.getBodyAsBlob();

    return body;

Then Im trying to run an apex anonymous for debugging

Blob b = api.getPDFReport('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NTkzOTgwMTgsImlhdCI6MTU1NjgwNjAxOCwiaXNzIjoiYXV0aDpwcm9kdWN0aW9uIiwic2VyYTpzaWQiOiJjZmEwNGQ4Zi01YTUzLTQ2ZGYtYjdiMi0zZThmY2RiOWI5NjMiLCJzZXJhOnR5cCI6ImF1dGgiLCJzdWIiOiIzNGZjNzYxZi1hNzM3LTRjMTYtYmIzNC1lNDBhZjczNjQzMTkifQ.qf0Ztxq84ZZYIGI4W_HwhhcN64IMY8yh0Ogi8YyJK60',


Attachment attach = new Attachment();
attach.Body = b;
attach.Name = String.valueOf('test.pdf');
attach.ParentId = '5005B000005NzihQAC';

insert attach;

The System.debug(b) is showing that the result is not empty and all the response headers are correct. But the attachment is empty.

Have anyone faced some similar situation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.