Guest User

Untitled

a guest
Mar 14th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. Attachment objPDF = [SELECT ID, Name, Body FROM Attachment where id = '00P0I00000u85YXUAY'
  2. LIMIT 1
  3. ];
  4. String strTargetURL = 'https://www.perfectaudit.com/api/v1/book/upload';
  5.  
  6. String boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW';
  7. string header1 = '--' + boundary + 'nContent-Disposition: form-data; name="pk"';
  8. String header1Encoded = EncodingUtil.base64Encode(Blob.valueOf(header1 + 'rnrn'));
  9. while (header1Encoded.endsWith('=')) {
  10. header1 += ' ';
  11. header1Encoded = EncodingUtil.base64Encode(Blob.valueOf(header1 + 'rnrn'));
  12. }
  13. String body1Encoded = EncodingUtil.base64Encode(Blob.valueOf('68749rn'));
  14. String last4Bytes = body1Encoded.substring(body1Encoded.length() - 4, body1Encoded.length());
  15. if (last4Bytes.endsWith('==')) {
  16. last4Bytes = last4Bytes.substring(0, 2) + '0K';
  17. body1Encoded = body1Encoded.substring(0, body1Encoded.length() - 4) + last4Bytes;
  18. } else if (last4Bytes.endsWith('=')) {
  19. last4Bytes = last4Bytes.substring(0, 3) + 'N';
  20. body1Encoded = body1Encoded.substring(0, body1Encoded.length() - 4) + last4Bytes;
  21. }
  22.  
  23. String header = '--' + boundary + 'nContent-Disposition: form-data; name="upload"; filename="' + objPDF.Name + '";nContent-Type: application/pdf';
  24. String footer = '--' + boundary + '--';
  25. String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header + 'rnrn'));
  26. while (headerEncoded.endsWith('=')) {
  27. header += ' ';
  28. headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header + 'rnrn'));
  29. }
  30. String bodyEncoded = EncodingUtil.base64Encode(Blob.valueOf('This is text file'));
  31. Blob bodyBlob = null;
  32. last4Bytes = bodyEncoded.substring(bodyEncoded.length() - 4, bodyEncoded.length());
  33. if (last4Bytes.endsWith('==')) {
  34. last4Bytes = last4Bytes.substring(0, 2) + '0K';
  35. bodyEncoded = bodyEncoded.substring(0, bodyEncoded.length() - 4) + last4Bytes;
  36. String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
  37. bodyBlob = EncodingUtil.base64Decode(header1Encoded + body1Encoded + headerEncoded + bodyEncoded + footerEncoded);
  38. } else if (last4Bytes.endsWith('=')) {
  39. last4Bytes = last4Bytes.substring(0, 3) + 'N';
  40. bodyEncoded = bodyEncoded.substring(0, bodyEncoded.length() - 4) + last4Bytes;
  41. footer = 'rn' + footer;
  42. String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
  43. bodyBlob = EncodingUtil.base64Decode(header1Encoded + body1Encoded + headerEncoded + bodyEncoded + footerEncoded);
  44. } else {
  45. footer = 'n' + footer;
  46. String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
  47. bodyBlob = EncodingUtil.base64Decode(header1Encoded + body1Encoded + headerEncoded + bodyEncoded + footerEncoded);
  48. }
  49.  
  50. HttpRequest req = new HttpRequest();
  51.  
  52. String userName = 'xxxxx';
  53. String password = 'xxxxx';
  54. Blob headerValue = Blob.valueOf(userName + ':' + password);
  55. String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
  56. req.setHeader('Authorization', authorizationHeader);
  57.  
  58. req.setHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
  59. req.setMethod('POST');
  60. req.setEndpoint(strTargetURL);
  61. req.setBodyAsBlob(bodyBlob);
  62. req.setTimeout(120000);
  63.  
  64. Http http = new Http();
  65. HTTPResponse res = http.send(req);
  66. system.debug(res.getBody());
Add Comment
Please, Sign In to add comment