0

I am trying to get the base64 encoded version of a Salesforce Classive File Upload field type via the Salesforce REST API from one org to another.

However when I query the rest API for the knowledge article type, I am returned an endpoint as the result for the Body__s field, and that endpoint does not return a base64 encoded version of the body either.

If I query the body__s via workbench query or through Dev Console query, I do get the base64 encoded body__s response.

Does the REST API not support querying on the Body__s field of Salesforce Classic Knowledge articles? Why can I query this through dev console and workbench, but not through REST Explorer or a call to the REST API?

The response I receive is:

My_Knowledge_Article_Attachment__Body__s":"/services/data/v48.0/sobjects/My_Article_Type__kav/XXX1F00000MASKXXXX/My_Knowledge_Article_Attachment__Body__s"} 

When going to the link provided in the response, we receive the following response:

HTTP/1.1 200 OK Date: Wed, 24 Jun 2020 19:51:31 GMT Strict-Transport-Security: max-age=31536002; includeSubDomains Public-Key-Pins-Report-Only: pin-sha256="MASK"; pin-sha256="MASK"; pin-sha256="MASK"; max-age=86400; includeSubDomains; report-uri="https://a.forcesslreports.com/hpkp-report/MASK"; Expect-CT: max-age=86400, report-uri="https://a.forcesslreports.com/Expect-CT-report/MASK" X-Robots-Tag: none Cache-Control: no-cache,must-revalidate,max-age=0,no-store,private Set-Cookie: BrowserId=MASK; domain=.salesforce.com; path=/; expires=Thu, 24-Jun-2021 19:51:31 GMT; Max-Age=31536000 Sforce-Limit-Info: api-usage=115/10470000 Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document Transfer-Encoding: chunked 
  • 1
    The reference link should be to a stream of that body's data. Some web clients like Workbench will read the stream and convert it into a base64 string for you, thus why you experience different results in different clients. Have you tried consuming the data in that link and reading it as base64? – cricketlang Jun 24 at 19:49
  • @cricketlang Any documentation or recommendations on consuming the data? See the above response we get back from a GET request on the Body__s link. – S.B. Jun 24 at 20:00
1

Make an additional HTTP GET request against the url returned to you in the My_Knowledge_Article_Attachment__Body__s field. Based off the headers you posted, this is returning a .docx content type, so we will essentially consume the docx as a blob, encode that blob to base64, then get the string value of that encoded blob.

When you go to get the body of this response, use getBodyAsBlob to read the data as a Blob, then use the encodingUtil.base64Encode() method to encode the Blob to a base64 String. This should end you with a base64 string of the file downloaded from the attachment body field.

Super rough example:

HttpRequest request = new HttpRequest();
String      url     = '{attachmentBodyUrl}';

request.setEndpoint(url);
request.setHeader('Authorization', 'OAuth '+ UserInfo.getSessionId());
request.setMethod('GET');

Http         fileClient   = new Http();
HttpResponse response     = fileClient.send(request);
Blob         rawResponse  = response.getBodyAsBlob();
String       base64String = EncodingUtil.base64Encode(rawResponse);
| improve this answer | |

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.