Skip to main content

Processing MultiPart/Form-Data file/Attachment using HTTP in Apex

If you need to process a file like images to some third party system using HTTP request in Apex , we need the Multipart/Form-data processing in the HTTP request.

In this example I am using salesforce chatter Rest API to fetch the content of a file and then passing that file content to the HTTP request.

This is just a sample code , you will have to enter the authorization tokens and endpoints.

Please note that processing files /Attachments using this may cause heap size governor limit also it counts against your daily limit of outbound webservice (HTTP) calls. 

1.Fetch File Content From Salesforce

 Blob bodyBlob = null;
 HTTPRequest req = new HTTPRequest();
 req.setEndpoint('<YourInstance>/services/data/v43.0/connect/files/fileId/content');
 req.setMethod('GET');
 req.setHeader('Authorization', 'Bearer  <Set oAuth Token>');
 Http http = new Http();
 HTTPResponse res = http.send(req);
 bodyBlob = blob.valueOf(res.getbody());
 uploadFile(bodyBlob ) ;   // See 2nd point for the method details

In an ideal scenario this should be done using some external service or middle-ware like Mulesoft but assuming you need to make a direct call from Salesforce this solution will make sense.

After you have the file content , you need to prepare a multipart/form-data request what the target system is expecting.

First test the request using postman. To choose file select form-date in body and then select file in the keys:-

Postman screen



You can click on the "Code" link on the postman top right corner to see the HTTP code. See the screenshot above.

The code should look like this in debug log :-

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="Nintex X logo.png";
Content-Type: image/png

�PNG


IHDR�� � -� pHYs ��~� FIDATx��=r"I �_���w iO ,92Ĝ`� �u���D !� �u�eN�`��,:�jn NПQ��
�OU��D�S��t?dvfUV+�2 @9�� �� �� �� �� �� �� � � � �
� ��9����VORWR'�����g�=p ����l�ܜ��:�Hz��拪�p+˲���H I� � N%-��X��ǺV�V_�]I����KI��l> A�E� P �%�^
6"\�R�%��XǑ��w��<�[������4� KQ�P�N$]KJ$ �x
�ټ�` � ^6R����y�.c ��y%����w + I�&�A7�kT�=b]���%�c@.I W��> ��x$�nZ ݴ���K����$}6�� ��c杻.M�������]�l�F ���
�N�]��t6O� �I� I >J������+��G�.���a� �1� Iw��'I��k�F�n��*�֒}7�\ K�4]���T� ��V�\� s�
%] ~���dI��D�� s���>�Ub�Jv�4� ���N�v �I6�[ ���3w��� ����5�ﱽ�� n1�D�ʸ/� ���\�ɕ����/�Z��l���$][+�#W ��EK�Kv/�gA����R�
�E0'Y"�?A��岚� '�T��Wa���
��b�.r!W� l ɺ�F m�#� ���� ��d ����F ��!�\�"�:�5�� �ni�,�Z�T�+�AnyA.�)�۔1������ r!W

------WebKitFormBoundary7MA4YWxkTrZu0gW

Content-Disposition: form-data; name="folder"
8F3CC6XCV

------WebKitFormBoundary7MA4YWxkTrZu0gW--

2. Create a method to send HTTP request with file data

public void uploadFile(Blob file){
       
     
String fileName = 'Companylogo.png';
String folderName = '8F3CC6XCV';
String boundary = '------WebKitFormBoundary7MA4YWxkTrZu0gW';

String fileHeader = '--'+boundary+'\nContent-Disposition: form-data; name="file";        filename="'+fileName+'";\nContent-Type: image/png \r\n';
String fileHeaderEncoded = EncodingUtil.base64Encode(Blob.valueOf(fileHeader));             
       
String filBodyEncoded = EncodingUtil.base64Encode(file);         

String folderHeader = '\r\n\r\n--'+boundary+'\nContent-Disposition: form-data; name="folder"';
String folderHeaderEncoded = EncodingUtil.base64Encode(Blob.valueof(folderHeader));

String folderBody = '\r\n'+folderName+'\r\n\r\n';
String folderBodyEncoded = EncodingUtil.base64Encode(Blob.valueof(folderBody));

String footer = '--'+boundary+'--';
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));

Blob bodyBlob = null;       
       
System.debug('Formatted request ::'+fileHeader+filBodyEncoded+folderHeader+folderBody+footer);

bodyBlob = EncodingUtil.base64Decode(fileHeaderEncoded+filBodyEncoded+folderHeaderEncoded+folderBodyEncoded+footerEncoded);
       
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
req.setMethod('POST');     
req.setEndpoint('http://localhost:8080/fileupload');
req.setHeader('Authorization', 'Bearer <enter token here>');
req.setBodyAsBlob(bodyBlob);
Http http = new Http();
HTTPResponse res = http.send(req);
       
}

Comments

Popular posts from this blog

Einstein Bot user authentication

Using Bot for data manipulation use case in your company requires need of implementing some extra security layer to make it spoof proof. One of exciting salesforce feature we have is the well known Einstein Bot which many companies have implemented. I am going to cover detail step-by-step implementation of User validation use case using encrypted token. STEP-1: Create a Site & Site User go to setup > Sites & Domains > Sites Create a Site and make your user as "Site Contact". This is a prerequisites for live agent or Embedded Service setup. STEP-2 : Create a Embedded Service(formerly snap-ins) go to Service setup > Embedded Service Create a new Embedded Service Deployment record and copy the embedded service code snipped generated in a notepad. STEP-3  : Create a Visualforce page to test the chatbot (it will simulate the actual web portal which is going to consume the embedded service snipped.) BotController.apxc public class BotControlle...

Dynamically populate lightning:combobox

In order to dynamically populate values in lightning:combobox using javascript we can create a list variable and push the values. We also require the values like ids associated to the particular option for back-end logic. For example  : If we want to show the list of contacts using lightning:combobox component where the option should display the contact name but the value should be the contact id which will be used for back-end logic For this we can create a temporary Map variable in our lightning component and set the value and label parameters of the lightning:combobox. Here is the code for this component Combo.app <aura:application extends="force:slds" access="global" description="Lightning Combo Demo"> <c:Combo_Cmpt/> </aura:application> Combo_Cmpt.cmp <aura:component implements="flexipage:availableForAllPageTypes" access="global" controller="ComboController"> <!-- ...

Use of wrapper class in lightning:datatable

As you guys know the wrapper class concept in visualforce pages , we generally use it to create a data-table which fetches data from different objects or if we want to redirect user to some other page on click of a link as one of the column of data-table.        For example we want a column "Account Name" on the data-table which is a link and once user clicks it should redirect respective account record. Or , suppose we want to display a column with some images or icons as a visual indicator Or what not. These requirements require us to use a wrapper on the lighting data-table (lightning:datatable) I am going to use my previous account search example ( Account Search Lightning Component ) and explain the use of wrapper. AccountSearchWrapper.app <aura:application extends="force:slds" access="global" >     <c:AccountSearchWrapper /> </aura:application> AccountSearchWrapper.cmp <aura:component controller="...