Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Attachment objPDF = [SELECT ID, Name, Body FROM Attachment where id = '00P0I00000u85YXUAY'
- LIMIT 1
- ];
- String strTargetURL = 'https://www.perfectaudit.com/api/v1/book/upload';
- String boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW';
- string header1 = '--' + boundary + 'nContent-Disposition: form-data; name="pk"';
- String header1Encoded = EncodingUtil.base64Encode(Blob.valueOf(header1 + 'rnrn'));
- while (header1Encoded.endsWith('=')) {
- header1 += ' ';
- header1Encoded = EncodingUtil.base64Encode(Blob.valueOf(header1 + 'rnrn'));
- }
- String body1Encoded = EncodingUtil.base64Encode(Blob.valueOf('68749rn'));
- String last4Bytes = body1Encoded.substring(body1Encoded.length() - 4, body1Encoded.length());
- if (last4Bytes.endsWith('==')) {
- last4Bytes = last4Bytes.substring(0, 2) + '0K';
- body1Encoded = body1Encoded.substring(0, body1Encoded.length() - 4) + last4Bytes;
- } else if (last4Bytes.endsWith('=')) {
- last4Bytes = last4Bytes.substring(0, 3) + 'N';
- body1Encoded = body1Encoded.substring(0, body1Encoded.length() - 4) + last4Bytes;
- }
- String header = '--' + boundary + 'nContent-Disposition: form-data; name="upload"; filename="' + objPDF.Name + '";nContent-Type: application/pdf';
- String footer = '--' + boundary + '--';
- String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header + 'rnrn'));
- while (headerEncoded.endsWith('=')) {
- header += ' ';
- headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header + 'rnrn'));
- }
- String bodyEncoded = EncodingUtil.base64Encode(Blob.valueOf('This is text file'));
- Blob bodyBlob = null;
- last4Bytes = bodyEncoded.substring(bodyEncoded.length() - 4, bodyEncoded.length());
- if (last4Bytes.endsWith('==')) {
- last4Bytes = last4Bytes.substring(0, 2) + '0K';
- bodyEncoded = bodyEncoded.substring(0, bodyEncoded.length() - 4) + last4Bytes;
- String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
- bodyBlob = EncodingUtil.base64Decode(header1Encoded + body1Encoded + headerEncoded + bodyEncoded + footerEncoded);
- } else if (last4Bytes.endsWith('=')) {
- last4Bytes = last4Bytes.substring(0, 3) + 'N';
- bodyEncoded = bodyEncoded.substring(0, bodyEncoded.length() - 4) + last4Bytes;
- footer = 'rn' + footer;
- String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
- bodyBlob = EncodingUtil.base64Decode(header1Encoded + body1Encoded + headerEncoded + bodyEncoded + footerEncoded);
- } else {
- footer = 'n' + footer;
- String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
- bodyBlob = EncodingUtil.base64Decode(header1Encoded + body1Encoded + headerEncoded + bodyEncoded + footerEncoded);
- }
- HttpRequest req = new HttpRequest();
- String userName = 'xxxxx';
- String password = 'xxxxx';
- Blob headerValue = Blob.valueOf(userName + ':' + password);
- String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
- req.setHeader('Authorization', authorizationHeader);
- req.setHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
- req.setMethod('POST');
- req.setEndpoint(strTargetURL);
- req.setBodyAsBlob(bodyBlob);
- req.setTimeout(120000);
- Http http = new Http();
- HTTPResponse res = http.send(req);
- system.debug(res.getBody());
Add Comment
Please, Sign In to add comment