Hello everyone, I am doing integration salesforce with box.com when i uplod file that time first i convert blob into base 64 encoded string then send file using rest api after uploading file my file in not readable form it meanse file is encoded. And i am sending file in body as a blob and also try in body but i can't sucess. so give me some idea that how i can solve this issue.
1 Answer
Uploading data into box.com uses content type as multipart/form-data.Posting multipart form data through apex HTTP has been challenging and there is some amount of work done in below blog
http://enreeco.blogspot.it/2013/01/salesforce-apex-post-mutipartform-data.html
All thanks to that blog and I was able to upload files to box .Below is code snippet that may hep you
private void uploadFile(String folderId,Attachment file,String token){
String boundary = '----------------------------741e90d31eff';
String header = '--'+boundary+'\nContent-Disposition: form-data; name="file"; filename="'+file.name+'";\nContent-Type: application/octet-stream';
// GW: Do not prepend footer with \r\n, you'll see why in a moment
// String footer = '\r\n--'+boundary+'--';
String footer = '--'+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);
// 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: 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 \n\n instead of \r\n replace == with 'oK'
last4Bytes = last4Bytes.substring(0,2) + '0K';
bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
// We have appended the \r\n 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 = '\r\n' + footer;
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
}
String sUrl = 'https://upload.box.com/api/2.0/files/content?parent_id='+folderId;
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
req.setMethod('POST');
req.setEndpoint(sUrl);
req.setBodyAsBlob(bodyBlob);
req.setTimeout(60000);
req.setHeader('Authorization', 'Bearer '+token);
req.setHeader('Content-Length',String.valueof(req.getBodyAsBlob().size()));
Http http = new Http();
HTTPResponse res = http.send(req);
}
Hopefully that helps you .
NOTE :If size of body becomes greater than 3MB ,the HTTP callout would fail as Apex does not support body size of more than 3MB .
-
sir above code is helpful for me and my file upload sucessful thanks .And i have one more issue that i want the downloadable link of uploaded file on box.com in response so what i have to do for to get link in response body just like in google drive i set approval_prompt=force;– sandeepCommented Aug 2, 2014 at 7:58
-
@sandeep Please upvote this if this helped..Also post this as other question i will respond . Commented Aug 2, 2014 at 8:33