+ Start a Discussion
prasanth sfdcprasanth sfdc 

HTTP repose converting attachment showing blank PDF pages.

I am trying to get the data PDF file from REST API, while storing it into the attachment object as PDF it is showing the empty PDF pages, kidnly help me to solve this issue. 

Note:- Encodeutil decode and encode not wokring while attachment creating converting the response,getBody() into attachment. 
 
string url = AWSUtility.viewS3Object('a1h0n00000187sc');
system.debug('The url is:' + url);

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(url);
request.setMethod('GET');
request.setHeader('Content-Type', 'application/pdf'); 
request.setHeader('Content-Encoding', 'UTF-8');
//req.setHeader('Content-Disposition', 'attachment; filename=' + fileName);
//req.setHeader('Connection', 'keep-alive');

HttpResponse response = http.send(request);
system.debug('The response is :' +response.getBody());
attachment att = new attachment();
att.parentId = 'a1h2g000000I1IN';
att.Name = 'Testing Raja2.pdf';
att.contentType = 'application/pdf'; 

att.body = blob.valueof(response.getBody());

//att.body = EncodingUtil.Base64Decode(response.getBody());
//att.body = EncodingUtil.base64Decode(response.getBody());
//att.body = EncodingUtil.base64Decode(blob.valueof(response.getBody()));
//att.body = EncodingUtil.base64Encode(blob.valueof(response.getBody()));
insert att;  (// attachment is getting created but, all empty pages only).

 
AnudeepAnudeep (Salesforce Developers) 
It should work fine based on this post. Are you seeing the raw response in the debug logs?
prasanth sfdcprasanth sfdc
Thank you for replying. It's working now after changed to response.getbodyasblob(). For downloading the Amazon S3 files into sfdc, we need to write like this .
AnudeepAnudeep (Salesforce Developers) 
Great to know. Please close-loop this by marking the relevant answer as Best so that It may help others in the community. Thank You!
maninder singh 50maninder singh 50

It took me many hours to figure this out. Its important to understand why  blob.valueof(response.getBody()) did not work and  response.getbodyasblob() works.

When using this 

blob.valueof(response.getBody())

response.getBody() is getting the body from the response in String format. the PDF file gets corrupted here itself. We the use blob.valueof() to convert the String to Blob for saving as contentversion.attachment.

response.getbodyasblob()

this methods retrieves the body from response directly as Blob. So no conversion takes place and pdf file is intact.

Why EncodingUtil.Base64Decode(response.getBody()) does not work here is because the response from the API is not a Base64 string. If you actually check the response you will find strange characters. Base64 only supports these characters:

Uppercase letters (indices 0-25): ABCDEFGHIJKLMNOPQRSTUVWXYZ
Lowercase letters (indices 26-51): abcdefghijklmnopqrstuvwxyz
Digits (indices 52-61): 0123456789
Special symbols (indices 62-63): +/

So trying to using EncodingUtil.Base64Decode(response.getBody()) with our response will give error of invalid characters. If you dig deeper and check the response received using System.debug(response.getBody()) you will realize that it is actually a PDF file format (the PDF specification for creating pdf files here (https://resources.infosecinstitute.com/topic/pdf-file-format-basic-structure/)). So we do not have to decode or uncompress the response in any way. We have to just retrieve the file as it is (as a blob) and save it to salesforce.

Example of (response.getBody()) you will see for these api responses which return PDF files. 

%PDF-1.4
%����
2 0 obj
<</ColorSpace/DeviceRGB/Subtype/Image/Height 200/Filter/FlateDecode/Type/XObject/DecodeParms<</Columns 420/Colors 3/Predictor 15/BitsPerComponent 8>>/Width 420/Length 6827/BitsPerComponent 8>>stream
x��    |���gfw�MBr�r�p1Ax)X�QZ,�*Z����h��`�XĖ��Z���)��A���A�=f�'��
�3��N�e��}?���d���;��3��
�����Bv� ;����