3

How to send PDF generated from VF page to EchoSign through Apex Code and REST API of EchoSign? I'm getting error as "Content Type not Supported/ UNSUPPORTED_MEDIA_TYPE". Any idea?

      String boundary = '----------------------------741e90d31eff';
      Blob file_body = getPageContent('asdv');
      String header = '--'+boundary+'\nContent-Disposition: form-data; name="file"; filename="'+'file_name'+'";\nContent-Type: application/octet-stream';
      String footer = '\r\n--'+boundary+'--';              
      String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
      while(headerEncoded.endsWith('='))
      {
       header+=' ';
       headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
      }
      String bodyEncoded = EncodingUtil.base64Encode(file_body);
      String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
      Blob bodyBlob = null;
      String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
      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);
      }

      HttpRequest req = new HttpRequest();
      req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
      req.setMethod('POST');
      String url = baseURL+ '/transientDocuments';
      req.setHeader('Access-Token',access_token);
      req.setEndpoint(url);
      req.setBodyAsBlob(bodyBlob);
      req.setTimeout(120000);

      Http http = new Http();
      HTTPResponse res = http.send(req);
      return res.getBody();
3
  • This is linked to my previous post salesforce.stackexchange.com/questions/50371/…
    – akash
    Sep 24 '14 at 11:30
  • Which endpoint are you using? The /transientDocuments one? You need to use a multipart/form-data request which Salesforce doesn't provide any native support for in Apex. You will need to create your request body manually using string concatenation. Sep 24 '14 at 11:40
  • @AlexTennant . Yes. I tried with concatenation of header+body+footer string with Encoding as well, but it didn't work . I checked with the blog :enreeco.blogspot.in/2013/01/…
    – Vignex
    Sep 24 '14 at 11:45

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.