I need to send the file attached in the salesforce object by using a rest call but the following line goes in error :

String body = EncodingUtil.base64Encode(cVersion.VersionData);  

cVersione is the contentVersion I receive as parameter in a method.

It works in the developer console but not in the apex method. What's wrong with that?

1 Answer 1


As the documentation for the EncodingUtil class points out, the base64Encode() method takes a Blob as an argument, not a string.

If you have a string and need to base64 encode it, you need to turn it into a Blob first. Something like EncodingUtil.base64Encode(Blob.valueOf(myString));

But you shouldn't need to do any of that here, as the versionData of a ContentVersion is already base64 encoded. The ContentVersion documentation is here and the relevant part of that is





Create, Nillable


Encoded file data. This field can't be set for links. The maximum file size you can upload via the SOAP API must be less than 50 MB. When a document is uploaded or downloaded via the API, it is converted to base64 and stored in VersionData. This conversion increases the document size by approximately 37%. You must account for the base64 conversion increase so that the file you plan to upload is less than 50 MB.

If it worked in the developer console, then I suspect the ContentVersion you were passing into the method had a null versionData.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .