I am using jsforce to query VersionData field in ContentVersion object. However, it returns me the URL to the base64 data instead of the actual data.

If I make a GET request to the URL (using the correct authorization headers), I do not get the correct base64 data. It returns some incomprehensible junk (see image below).

GET request to the URL returned by VersionData

I have also used 'request' method from 'Connection' class to make the http request but it returns the same data.

const t = await conn.request(url)
console.log(t) // the same data

I want to convert the base64 data into Buffer (node.js) and send it to the the browser client.

  • Hi @SumitIsrani, did you see the question here?. The REST API will not return base64 content, but the SOAP API will. It's likely the REST endpoint is returning a BLOB for the file (it looks like it's a PNG from your screenshot) so it should be possible to convert it with an encoding, it's just a matter of finding the proper one.
    – nbrown
    May 20 '20 at 13:45
  • @nbrown yes I had seen the question. I was not able to figure out how to use SOAP API in this context. Thanks. May 20 '20 at 15:13

The "incomprehensible junk" is the actual binary content of the file.

The URL that you receive in ContentVersion's Body field is an sObject Blob Retrieve endpoint. From that documentation:

Because blob fields contain binary data, you can't use JSON or XML to retrieve this data.

There's no need to perform Base64 decoding because you are directly receiving the binary content.

  • 1
    I tried converting this data into Buffer with Buffer.from(data, 'binary') but it didn't give me the correct results. Thanks anyways. May 20 '20 at 15:16

I am not sure why this doesn't give me correct data but I have found a work around.

I am using fetch-base64 package to make request to this particular URL. I am passing the correct authorization header along with the request.

    const doFetchRemote = await fetch.remote({ 
      url: instanceUrl + contentVersionObj.VersionData, 
      headers: { 
        'Authorization': 'Bearer ' + accessToken 

I am able to get correct data in base64. Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.