从外部Web服务获取PDF并将其存储为附件会创建空pdf

时间:2019-05-02 15:51:47

标签: web-services apex salesforce-lightning

我必须从外部Web服务获取pdf并将其存储为对象的附件。

我正在使用以下代码获取pdf

    public override Blob getPDFReport(String token, String jobId) {
        HttpRequest req = new HttpRequest();
        req.setMethod(METHOD_GET);
        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);
        req.setHeader('Transfer-Encoding','chunked');
        req.setHeader('X-Content-Type-Options','nosniff');
        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',
                      '7f194a97-743d-412b-a54b-4b97c0abcb0a');

System.debug(b);

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

insert attach;

System.debug(b)显示结果不为空,并且所有响应标头均正确。但是附件为空。

有人遇到过类似情况吗?

0 个答案:

没有答案