Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **cURL Request:**
- POST /api/v1/book/info HTTP/1.1
- Host: www.perfectaudit.com
- Authorization: Basic Y2hpdGl6QGxlbmRpbmd4bC5jb206U3VjY2Vzc0AxMDE=
- Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
- Cache-Control: no-cache
- Postman-Token: 875af4d4-616e-156f-1af1-d67fd925217a
- ------WebKitFormBoundary7MA4YWxkTrZu0gW
- Content-Disposition: form-data; name="pk"
- 71704
- ------WebKitFormBoundary7MA4YWxkTrZu0gW
- Content-Disposition: form-data; name="upload"; filename="eStmt_2017-11-30 (1).pdf"
- Content-Type: application/pdf
- ------WebKitFormBoundary7MA4YWxkTrZu0gW--
- **Response**
- {"status": 400, "message": "required key not provided @ data['pk']", "code": 1003}
- **HTTP Request in Apex:**
- String boundary = '------WebKitFormBoundary7MA4YWxkTrZu0gW';
- String header = boundary+'nContent-Disposition: form-data; name="pk"nn'+bookId;
- `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+'";
- System.debug('header>>> '+header);
- // GW: Do not prepend footer with rn, you'll see why in a moment
- // String footer = 'rn--'+boundary+'--';
- String footer = 'nnn'+boundary+'--';
- String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'rnrn'));
- while(headerEncoded.endsWith('=')){
- header+=' ';
- headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'rnrn'));
- }
- String bodyEncoded = EncodingUtil.base64Encode(file_body);
- // GW: Do not encode footer yet
- // String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
- Blob bodyBlob = null;
- String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
- // GW: Replacing this entire section
- // GW: replacement section to get rid of padding without corrupting data
- if(last4Bytes.endsWith('==')) {
- // The '==' sequence indicates that the last group contained only one 8 bit byte
- // 8 digit binary representation of CR is 00001101
- // 8 digit binary representation of LF is 00001010
- // Stitch them together and then from the right split them into 6 bit chunks
- // 0000110100001010 becomes 0000 110100 001010
- // Note the first 4 bits 0000 are identical to the padding used to encode the
- // second original 6 bit chunk, this is handy it means we can hard code the response in
- // The decimal values of 110100 001010 are 52 10
- // The base64 mapping values of 52 10 are 0 K
- // See http://en.wikipedia.org/wiki/Base64 for base64 mapping table
- // Therefore, we replace == with 0K
- // Note: if using nn instead of rn replace == with 'oK'
- last4Bytes = last4Bytes.substring(0,2) + '0K';
- bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
- // We have appended the rn to the Blob, so leave footer as it is.
- String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
- bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
- } else if(last4Bytes.endsWith('=')) {
- // '=' indicates that encoded data already contained two out of 3x 8 bit bytes
- // We replace final 8 bit byte with a CR e.g. r
- // 8 digit binary representation of CR is 00001101
- // Ignore the first 2 bits of 00 001101 they have already been used up as padding
- // for the existing data.
- // The Decimal value of 001101 is 13
- // The base64 value of 13 is N
- // Therefore, we replace = with N
- // Note: if using n instead of r replace = with 'K'
- last4Bytes = last4Bytes.substring(0,3) + 'N';
- bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
- // We have appended the CR e.g. r, still need to prepend the line feed to the footer
- footer = 'n' + footer;
- String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
- bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
- } else {
- // Prepend the CR LF to the footer
- footer = 'rn' + footer;
- String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
- bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
- }
- HttpRequest req = new HttpRequest();
- String userName = ocrolusSettings.lendingxl__UserName__c;
- String password = ocrolusSettings.lendingxl__Password__c;
- Blob headerValue = Blob.valueOf(userName+ ':' +password);
- String authorizationHeader = 'Basic ' +EncodingUtil.base64Encode(headerValue);
- System.debug('authorizationHeader>>>'+authorizationHeader);
- req.setHeader('Authorization', authorizationHeader);
- System.debug('boundary>>>'+boundary);
- req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
- //req.setHeader('Content-Type','multipart/form-data; boundary='+headerEncoded);
- req.setMethod(method);
- req.setEndpoint(ocrolusSettings.lendingxl__EndPoint__c+ resourceName);//
- //req.setEndpoint(reqEndPoint);
- req.setBodyAsBlob(bodyBlob);
- req.setTimeout(120000);
- Http http = new Http();
- System.debug('req>>> '+req);
- HTTPResponse res = http.send(req);
- response =res.getBody();
Add Comment
Please, Sign In to add comment