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:-
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);
}
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
Post a Comment