4

How do I send a pdf through REST call out of Salesforce? Should we use sites? Any idea?

1
  • 1
    You can make RESTful HTTP calls out from Apex code via the HttpRequest class. So as long as you are initiating the request e.g. from a controller you do not need things like Force.com Sites.
    – Keith C
    Sep 24, 2014 at 9:56

2 Answers 2

5

Apex controller is enough to do this:

// Sample code only 

HttpRequest req = new HttpRequest();
// set endpoint destination
req.setEndpoint( 'http://mydestination.com');
req.setMethod('POST');
req.setHeader('Authorization', 'OAuth ' + access);
req.setHeader('Content-Type','application/json');
// get the PDf. Can be other ways
Blob pdfBody = Page.SamplePage.getContentAsPDF();
// convert to base64 string
String base64PDF = EncodingUtil.base64Encode(pdfBody);
// Set PDF body here
req.setBody(base64PDF);
Http http = new Http();
// send the request
HttpResponse response = http.send(req);

For more detail: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http_httprequest.htm

1
  • Any File Size MB or KB Limitations in Salesforce?
    – shyam
    Jan 19, 2022 at 13:26
0

this is how we can set a pdf file in a rest api call in apex salesforce -

public static HTTPResponse uploadFile(Attachmnet file)
{
  String boundary = '__boundary__xxx';
  String header = '--'+boundary+'\n';
  body += 'Content-Disposition: form-data; name="data"; filename="'+file.name
    +'"\nContent-Type: application/octet-stream';
 
  String footer = '\n--'+boundary+'--';
   
  // no trailing padding on header by adding ' ' before the last "\n\n" characters
  String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\n\n'));
  //this ensures no trailing "=" padding
  while(headerEncoded.endsWith('='))
  {
   header+=' ';
   headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\n\n'));
  }
  //base64 encoded body
  String bodyEncoded = EncodingUtil.base64Encode(file.body);
  //base64 encoded footer
  String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
   
  Blob bodyBlob = null;
  //last encoded body bytes
  String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
  //if the last 4 bytes encoded base64 ends with the padding character (= or ==) then re-encode those bytes with the footer
  //to ensure the padding is added only at the end of the body
  if(last4Bytes.endsWith('='))
  {
   Blob decoded4Bytes = EncodingUtil.base64Decode(last4Bytes);
   HttpRequest tmp = new HttpRequest();
   tmp.setBodyAsBlob(decoded4Bytes);
   String last4BytesFooter = tmp.getBody()+footer;   
   bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded.substring(0,bodyEncoded.length()-4)+EncodingUtil.base64Encode(Blob.valueOf(last4BytesFooter)));
  }
  else
  {
   bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
  }
   
  if(bodyBlob.size()>3000000)
  { 
   //this a "public class CustomException extends Exception{}"
   throw new CustomException('File size limit is 3 MBytes');
  }
   
  HttpRequest req = new HttpRequest();
  req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
  req.setMethod('POST');
  req.setEndpoint('http://posttestserver.com/post.php?dir=watchdox');  
  req.setBodyAsBlob(bodyBlob);
  req.setTimeout(60000);
  req.setHeader('Content-Length',String.valueof(req.getBodyAsBlob().size()));
  Http http = new Http();
  HTTPResponse res = http.send(req);
  return res;
}

more detailed explanation of the above code you can find from belo source - http://enreeco.blogspot.com/2013/01/salesforce-apex-post-mutipartform-data.html

You must log in to answer this question.

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