You need to sign in to do that
Don't have an account?
Downloading a document using Rest API
Hi
I want to download a word document stored on Box( External Storage) into my system using Rest API calls
before downloading i need to decode the document as its uploaded using base 64 Encoding Util class
but when i decode it back it gives me Blob[0] , Perhaps its not getting decoded
Can someone help me on this ?
I am using below code :
private String downloadFile(){
system.debug('Call DownloadFile');
Blob body;
String sFilename='';
Http h = new Http();
HttpRequest req = new HttpRequest();
string endPointValue = 'https://api.box.com/2.0/files/10777849377/content';
req.setEndpoint(endPointValue);
req.setHeader('Authorization', 'Bearer ' + accessToken);
req.setMethod('GET');
System.debug('req '+req);
HttpResponse res = h.send(req);
system.debug('res '+res);
// if(!Test.isRunningTest()){
system.debug('Body '+res.getBody());
system.debug('-get headers---'+res.getHeader('Location'));
body= Encodingutil.base64Decode(res.getBody());
system.debug('Body Decoded '+body);
if(res.getHeader('Location')!=null)
{
return res.getHeader('Location')+'';
} else {
return '';
}
}
Hello,
Use method base64Encode(Blob) to convert a Blob to an unencoded String representing its normal form.
Refer: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_EncodingUtil_base64Encode.htm
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_EncodingUtil_instance_methods.htm
hi Vinita
I guess you are telling about Upload .
I have already uploaded the document using base64Encode(Blob)
Now i want to decode and download the same document
I am trying to decode like this :
HttpResponse res = h.send(req);
system.debug('res '+res);
// if(!Test.isRunningTest()){
system.debug('Body '+res.getBody());
system.debug('-get headers---'+res.getHeader('Location'));
body= Encodingutil.base64Decode(res.getBody());
system.debug('Body Decoded '+body);
but the response body is blank , not sure why ?
can anyone help ?
Hello,
So for header are getting reponse, is that not blank? Try converting blob to string:
String bodyString = body.toString();