Guest User

Untitled

a guest
Mar 7th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. **cURL Request:**
  2.  
  3. POST /api/v1/book/info HTTP/1.1
  4. Host: www.perfectaudit.com
  5. Authorization: Basic Y2hpdGl6QGxlbmRpbmd4bC5jb206U3VjY2Vzc0AxMDE=
  6. Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
  7. Cache-Control: no-cache
  8. Postman-Token: 875af4d4-616e-156f-1af1-d67fd925217a
  9.  
  10. ------WebKitFormBoundary7MA4YWxkTrZu0gW
  11. Content-Disposition: form-data; name="pk"
  12.  
  13. 71704
  14. ------WebKitFormBoundary7MA4YWxkTrZu0gW
  15. Content-Disposition: form-data; name="upload"; filename="eStmt_2017-11-30 (1).pdf"
  16. Content-Type: application/pdf
  17.  
  18.  
  19. ------WebKitFormBoundary7MA4YWxkTrZu0gW--
  20.  
  21.  
  22. **Response**
  23.  
  24. {"status": 400, "message": "required key not provided @ data['pk']", "code": 1003}
  25.  
  26. **HTTP Request in Apex:**
  27.  
  28. String boundary = '------WebKitFormBoundary7MA4YWxkTrZu0gW';
  29. String header = boundary+'nContent-Disposition: form-data; name="pk"nn'+bookId;
  30. `enter code here`header += 'n'+boundary+'nContent-Disposition: form-data; name="upload"; filename="'+file_name+'"nContent-Type: application/pdf'; //application/octet-stream multipart/form-data //pk="'+bookId+'";
  31.  
  32. System.debug('header>>> '+header);
  33. // GW: Do not prepend footer with rn, you'll see why in a moment
  34. // String footer = 'rn--'+boundary+'--';
  35. String footer = 'nnn'+boundary+'--';
  36. String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'rnrn'));
  37. while(headerEncoded.endsWith('=')){
  38. header+=' ';
  39. headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'rnrn'));
  40. }
  41.  
  42.  
  43. String bodyEncoded = EncodingUtil.base64Encode(file_body);
  44.  
  45.  
  46. // GW: Do not encode footer yet
  47. // String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
  48.  
  49. Blob bodyBlob = null;
  50. String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
  51.  
  52. // GW: Replacing this entire section
  53.  
  54. // GW: replacement section to get rid of padding without corrupting data
  55. if(last4Bytes.endsWith('==')) {
  56. // The '==' sequence indicates that the last group contained only one 8 bit byte
  57. // 8 digit binary representation of CR is 00001101
  58. // 8 digit binary representation of LF is 00001010
  59. // Stitch them together and then from the right split them into 6 bit chunks
  60. // 0000110100001010 becomes 0000 110100 001010
  61. // Note the first 4 bits 0000 are identical to the padding used to encode the
  62. // second original 6 bit chunk, this is handy it means we can hard code the response in
  63. // The decimal values of 110100 001010 are 52 10
  64. // The base64 mapping values of 52 10 are 0 K
  65. // See http://en.wikipedia.org/wiki/Base64 for base64 mapping table
  66. // Therefore, we replace == with 0K
  67. // Note: if using nn instead of rn replace == with 'oK'
  68. last4Bytes = last4Bytes.substring(0,2) + '0K';
  69. bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
  70. // We have appended the rn to the Blob, so leave footer as it is.
  71. String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
  72. bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
  73. } else if(last4Bytes.endsWith('=')) {
  74. // '=' indicates that encoded data already contained two out of 3x 8 bit bytes
  75. // We replace final 8 bit byte with a CR e.g. r
  76. // 8 digit binary representation of CR is 00001101
  77. // Ignore the first 2 bits of 00 001101 they have already been used up as padding
  78. // for the existing data.
  79. // The Decimal value of 001101 is 13
  80. // The base64 value of 13 is N
  81. // Therefore, we replace = with N
  82. // Note: if using n instead of r replace = with 'K'
  83. last4Bytes = last4Bytes.substring(0,3) + 'N';
  84. bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
  85. // We have appended the CR e.g. r, still need to prepend the line feed to the footer
  86. footer = 'n' + footer;
  87. String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
  88. bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
  89. } else {
  90. // Prepend the CR LF to the footer
  91. footer = 'rn' + footer;
  92. String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
  93. bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
  94. }
  95.  
  96. HttpRequest req = new HttpRequest();
  97.  
  98. String userName = ocrolusSettings.lendingxl__UserName__c;
  99. String password = ocrolusSettings.lendingxl__Password__c;
  100. Blob headerValue = Blob.valueOf(userName+ ':' +password);
  101. String authorizationHeader = 'Basic ' +EncodingUtil.base64Encode(headerValue);
  102. System.debug('authorizationHeader>>>'+authorizationHeader);
  103. req.setHeader('Authorization', authorizationHeader);
  104.  
  105. System.debug('boundary>>>'+boundary);
  106.  
  107. req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
  108.  
  109. //req.setHeader('Content-Type','multipart/form-data; boundary='+headerEncoded);
  110. req.setMethod(method);
  111. req.setEndpoint(ocrolusSettings.lendingxl__EndPoint__c+ resourceName);//
  112. //req.setEndpoint(reqEndPoint);
  113. req.setBodyAsBlob(bodyBlob);
  114. req.setTimeout(120000);
  115.  
  116. Http http = new Http();
  117.  
  118. System.debug('req>>> '+req);
  119. HTTPResponse res = http.send(req);
  120. response =res.getBody();
Add Comment
Please, Sign In to add comment