You need to sign in to do that
Don't have an account?

Unable to upload .docx file from salesforce attachments to box.com using Rest API
Hi,
I want to upload salesforce attachments to box.com account using Rest API. For authentication I am using Auth Provider. I have created a named credential "TestBox" which uses this Auth Provider and url "https://upload.box.com/api/2.0/". I am able to uplaod .txt files using this. But while uploading .docx file it shows "Script-thrown exception".
I am able to upload .docx file by hardcoding Endpoint. But here I want to use named credentials. Can anyone help.
This is my code::
attachment file1= [SELECT Id, Name, Body FROM attachment where name like '%COS%' LIMIT 1];
system.debug('file1'+file1);
String x= uploadFileToBox('0', file1 );
system.debug('x'+x);
public static String uploadFileToBox(String strFolderId,attachment file)
{
String fileId ;
String boundary = '----------------------------741e90d31eff';
String header = '--'+boundary+'\nContent-Disposition: form-data; name="file"; filename="'+file.name+'";\nContent-Type: multipart/form-data;'+'\nnon-svg='+True;
String footer = '--'+boundary+'--';
String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
//HttpResponse res;
String strFileId;
while(headerEncoded.endsWith('='))
{
header+=' ';
headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
}
String bodyEncoded = EncodingUtil.base64Encode(file.body);
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('=='))
{
last4Bytes = last4Bytes.substring(0,2) + '0K';
bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
}
else if(last4Bytes.endsWith('='))
{
last4Bytes = last4Bytes.substring(0,3) + 'N';
bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
footer = '\n' + footer;
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
}
else
{
footer = '\r\n' + footer;
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
}
String sUrl = 'callout:TestBox/files/content?parent_id='+strFolderId;
HttpRequest req = new HttpRequest();
//req.setHeader('Content-Type','multipart/form-data;non_svg='+True+';boundary='+boundary);
req.setHeader('Content-Type','application/msword');
req.setMethod('POST');
req.setEndpoint(sUrl);
req.setBodyAsBlob(bodyBlob);
req.setTimeout(60000);
req.setHeader('Content-Length',String.valueof(req.getBodyAsBlob().size()));
Http http = new Http();
system.debug('*****size****'+req.getBodyAsBlob().size());
if(Test.isRunningTest()) {
// Create a fake response
//res = new HttpResponse();
//res.setStatusCode(201);
}
else {
try{
HttpResponse res = http.send(req);
System.debug('res = ' + res);
System.debug('res = ' + res.getBody());
Jsonparser parser = Json.createParser(res.getBody());
system.debug('parser '+parser );
while (parser.nextToken() != null)
{
parser.nextValue();
String fieldName = parser.getCurrentName();
String fieldValue = parser.getText();
if(fieldName == 'id')
{
fileId = fieldValue;
system.debug('fileId '+fileId );
return fileId;
}
}
return fileId;
}catch (System.CalloutException e){
System.debug('ERROR:' + e);
}
}
return fileId;
}
Thanks,
Adheena
I want to upload salesforce attachments to box.com account using Rest API. For authentication I am using Auth Provider. I have created a named credential "TestBox" which uses this Auth Provider and url "https://upload.box.com/api/2.0/". I am able to uplaod .txt files using this. But while uploading .docx file it shows "Script-thrown exception".
I am able to upload .docx file by hardcoding Endpoint. But here I want to use named credentials. Can anyone help.
This is my code::
attachment file1= [SELECT Id, Name, Body FROM attachment where name like '%COS%' LIMIT 1];
system.debug('file1'+file1);
String x= uploadFileToBox('0', file1 );
system.debug('x'+x);
public static String uploadFileToBox(String strFolderId,attachment file)
{
String fileId ;
String boundary = '----------------------------741e90d31eff';
String header = '--'+boundary+'\nContent-Disposition: form-data; name="file"; filename="'+file.name+'";\nContent-Type: multipart/form-data;'+'\nnon-svg='+True;
String footer = '--'+boundary+'--';
String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
//HttpResponse res;
String strFileId;
while(headerEncoded.endsWith('='))
{
header+=' ';
headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
}
String bodyEncoded = EncodingUtil.base64Encode(file.body);
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('=='))
{
last4Bytes = last4Bytes.substring(0,2) + '0K';
bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
}
else if(last4Bytes.endsWith('='))
{
last4Bytes = last4Bytes.substring(0,3) + 'N';
bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
footer = '\n' + footer;
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
}
else
{
footer = '\r\n' + footer;
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
}
String sUrl = 'callout:TestBox/files/content?parent_id='+strFolderId;
HttpRequest req = new HttpRequest();
//req.setHeader('Content-Type','multipart/form-data;non_svg='+True+';boundary='+boundary);
req.setHeader('Content-Type','application/msword');
req.setMethod('POST');
req.setEndpoint(sUrl);
req.setBodyAsBlob(bodyBlob);
req.setTimeout(60000);
req.setHeader('Content-Length',String.valueof(req.getBodyAsBlob().size()));
Http http = new Http();
system.debug('*****size****'+req.getBodyAsBlob().size());
if(Test.isRunningTest()) {
// Create a fake response
//res = new HttpResponse();
//res.setStatusCode(201);
}
else {
try{
HttpResponse res = http.send(req);
System.debug('res = ' + res);
System.debug('res = ' + res.getBody());
Jsonparser parser = Json.createParser(res.getBody());
system.debug('parser '+parser );
while (parser.nextToken() != null)
{
parser.nextValue();
String fieldName = parser.getCurrentName();
String fieldValue = parser.getText();
if(fieldName == 'id')
{
fileId = fieldValue;
system.debug('fileId '+fileId );
return fileId;
}
}
return fileId;
}catch (System.CalloutException e){
System.debug('ERROR:' + e);
}
}
return fileId;
}
Thanks,
Adheena

I am able to upload .docx file of size 20kb while using named credentials for authentication. For files having size 50KB or more, I am unable to upload it while using named credentials for authentication. If I use access token for authentication I can upload the same. How do I upload .docx file of 50kb and more while using named credentials for authentication?